BZOJ 1355: [Baltic2009]Radio Transmission

来源:互联网 发布:新闻数据库表 编辑:程序博客网 时间:2024/05/20 20:18

Description

给你一个字符串,它是由某个字符串不断自我连接形成的。 但是这个字符串是不确定的,现在只想知道它的最短长度是多少.

Input

第一行给出字符串的长度,1 < L ≤ 1,000,000. 第二行给出一个字符串,全由小写字母组成.

Output

输出最短的长度

Sample Input

8

cabcabca

Sample Output

3

HINT

对于样例,我们可以利用”abc”不断自我连接得到”abcabcabc”,读入的cabcabca,是它的子串

分析

答案为:n-next[n]

代码

#include <bits/stdc++.h>#define N 1000005char ch[N];int next[N];int n;int read(){    int x = 0, f = 1;    char ch = getchar();    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}    return x * f;}void getNext(){    next[0] = -1;    int i = 2, j = 0;    while (i <= n)    {        if (ch[i] == ch[j + 1] || j == -1)        {            next[i] = ++j;            i++;        }        else j = next[j];    }}int main(){    n = read();    scanf("%s", ch + 1);    getNext();    printf("%d\n", n - next[n]);}
0 0
原创粉丝点击