hust1010 The Minimum Length
地址:http://acm.hust.edu.cn/problem/show/1010
题目:
1010 - The Minimum Length
Time Limit: 1s Memory Limit: 128MB
Submissions: 2502 Solved: 925
- DESCRIPTION
- 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.
- INPUT
- Multiply 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.
- OUTPUT
- For each line, output an integer, as described above.
- SAMPLE INPUT
-
bcabcab efgabcdefgabcde
- SAMPLE OUTPUT
-
3 7
思路:kmp+最小循环节1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int nt[K]; 15 char sa[K],sb[K]; 16 17 void kmp_next(char *T,int *nt) 18 { 19 nt[0]=0; 20 for(int i=1,j=0,len=strlen(T);i<len;i++) 21 { 22 while(j&&T[j]!=T[i])j=nt[j-1]; 23 if(T[j]==T[i])j++; 24 nt[i]=j; 25 } 26 } 27 int kmp(char *S,char *T,int *nt) 28 { 29 int ans=0; 30 kmp_next(T,nt); 31 int ls=strlen(S),lt=strlen(T); 32 for(int i=0,j=0;i<ls;i++) 33 { 34 while(j&&S[i]!=T[j])j=nt[j-1]; 35 if(S[i]==T[j])j++; 36 if(j==lt) 37 ans++,j=0; 38 } 39 return ans; 40 } 41 int main(void) 42 { 43 int t; 44 while(scanf("%s",sa)==1) 45 { 46 kmp_next(sa,nt); 47 int len=strlen(sa); 48 int ans=len-nt[len-1]; 49 printf("%d\n",ans); 50 } 51 return 0; 52 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。