HDU 1358

桃扇骨 2022-09-22 13:52 90阅读 0赞

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.

Output

For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

  1. 3
  2. aaa
  3. 12
  4. aabaabaabaab
  5. 0

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. char a[1000001];
  6. int nxt[1000001];
  7. void getnxt()
  8. {
  9. int l=strlen(a);
  10. int i=0;
  11. int j=-1;
  12. nxt[0]=-1;
  13. while(i<l)
  14. {
  15. if(j==-1||a[i]==a[j])
  16. {
  17. i++;
  18. j++;
  19. nxt[i]=j;
  20. }
  21. else
  22. j=nxt[j];
  23. }
  24. }
  25. int main()
  26. {
  27. int i,j,k=1,m,n,l;
  28. while(~scanf("%d",&n),n)
  29. {
  30. memset(nxt,0,sizeof(nxt));
  31. scanf("%s",a);
  32. printf("Test case #%d\n",k++);
  33. l=strlen(a);
  34. getnxt();
  35. for(i=2;i<=l;i++)
  36. {
  37. //printf("(%d)\n",nxt[3]);
  38. int t=i-nxt[i];
  39. if(i/t<=1) continue;
  40. else if(i%t==0)
  41. {
  42. printf("%d %d\n",i,i/t);
  43. }
  44. }
  45. puts("");
  46. }
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1358(kmp)

    题意:给出一个数字n,接下来一行是一个字符串,n是这个字符串的长度。求这个字符串的所有是循环字符串的前缀。 kmp中的next数组只得是第i个字符匹配错误,向前跳的位置nex