博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1358
阅读量:5023 次
发布时间:2019-06-12

本文共 2337 字,大约阅读时间需要 7 分钟。

Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11371    Accepted Submission(s): 5313

Problem 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
3 aaa 12 aabaabaabaab 0
 

 

Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
 
题意:输出循环当前的位置,并且输出循环节的个数。
aabaabaabaab例子中:aa时有两个a,并且第二个a出现在第二个位置。
aabaab有两个aab,第二个aab在第六个位置结束,所以有6 2
aabaabaab有三个aab,最后一个结束在第九个位置,所以有9 3
以此类推,有12 4
1 #include
2 using namespace std; 3 const int maxn=1000006; 4 char str[maxn]; 5 int next[maxn]; 6 int n; 7 8 void get_next() 9 {10 next[0]=-1;11 int tmp=-1,i=0;12 while(i!=n){13 if(tmp==-1||str[i]==str[tmp])14 next[++i]=++tmp;15 else16 tmp=next[tmp];17 }18 }19 20 int main()21 {22 int nCase=0;23 while( ~scanf("%d",&n)&&n){24 scanf("%s",str);25 get_next();26 // for(int i=0;i<=n;i++)27 // printf("%d ",next[i]);28 printf("Test case #%d\n",++nCase);29 for(int i=1;i<=n;i++){30 int length=i-next[i];31 if(i!=length&&i%length==0){32 int ans=i/(i-next[i]);33 printf("%d %d\n",i,ans);34 }35 }36 printf("\n");37 }38 return 0;39 }

 

转载于:https://www.cnblogs.com/ZQUACM-875180305/p/9283023.html

你可能感兴趣的文章
大数运算(1)—— 加法篇
查看>>
Swift--错误集:couldn’t be opened because you don’t have permission to view it
查看>>
javascript引擎执行的过程的理解--语法分析和预编译阶段
查看>>
Java_基础—递归
查看>>
Apache+Tomcat构建Tomcat负载均衡集群
查看>>
学会去分享,也许会走得更远
查看>>
redis基本命令
查看>>
inherited 的研究。
查看>>
Hibernate由model类自动同步数据库表结构
查看>>
Android 架构 2.界面
查看>>
SPEL语言-Spring Expression Language
查看>>
设置nginx中文件上传的大小限制度
查看>>
C# 课堂总结3-语句
查看>>
精读《Scheduling in React》
查看>>
[Leetcode] 4Sum
查看>>
java.lang.StackOverflowError 解决方法
查看>>
hibernate事务控制
查看>>
[WCF编程]10.操作:请求/应答操作
查看>>
xfire实现webservice客户端之测试关注点
查看>>
在MySQL数据库中出现中文显示乱码
查看>>