首页 > 代码库 > F - The Minimum Length
F - The Minimum Length
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.InputMultiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.OutputFor each line, output an integer, as described above.Sample Input
bcabcab efgabcdefgabcde
Sample Output
3 7
#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<string> #include<cstring> using namespace std; #define MAXN 1000001 typedef long long LL; /* 给定一个串,找最短循环节 */ char s[MAXN]; int Next[MAXN]; void kmp_pre(int m) { int j,k; j = 0;k = Next[0] = -1; while(j<m) { if(k==-1||s[j]==s[k]) Next[++j] = ++k; else k = Next[k]; } } int main() { while(scanf("%s",s)!=EOF) { int l = strlen(s); kmp_pre(l); int ans = l - Next[l]; printf("%d\n",ans); } }
F - The Minimum Length