您的位置:首页 > 其它

BZOJ 1355: [Baltic2009]Radio Transmission

2017-05-01 21:30 323 查看

Description

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

Input

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

Output

输出最短的长度

Sample Input

8

cabcabca

Sample Output

3

HINT

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

分析

答案为:n-next

代码

#include <bits/stdc++.h>

#define N 1000005

char ch
;
int next
;

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
);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: