Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2258    Accepted Submission(s): 1117

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 AK , 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
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn=; char str[maxn];
int N,fail[maxn]; void getfail()
{
int n=strlen(str);
fail[]=fail[]=;
for(int i=;i<n;i++)
{
int j=fail[i];
while(j&&str[i]!=str[j]) j=fail[j];
fail[i+]=(str[i]==str[j])?(j+):;
}
} int main()
{
int cas=;
while(scanf("%d",&N)!=EOF&&N)
{
scanf("%s",str);
printf("Test case #%d\n",cas++);
getfail();
for(int i=;i<=N;i++)
{
if(fail[i]&&(i%(i-fail[i])==))
{
printf("%d %d\n",i,i/(i-fail[i]));
}
}
putchar();
}
return ;
}

最新文章

  1. Scrapy框架实现爬虫
  2. CSS 圆角
  3. 想从事分布式系统,计算,hadoop等方面,需要哪些基础,推荐哪些书籍?--转自知乎
  4. 控制台打印出event对象时,对象里面的currentTarget为null
  5. [ Iptables ] Linux开启防火墙,切记仔细确定每个端口
  6. hdu 5146 Sequence
  7. 转载 C#使用Salt + Hash来为密码加密
  8. 多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)
  9. Python Object Graphs — objgraph 1.7.2 documentation
  10. C/C++数据对齐汇总
  11. jQuery扩展两类函数(对象调用,静态调用)
  12. 鼠标滑过切换div显示(鼠标事件)
  13. 使用labelme制作自己的数据集
  14. kvm虚拟化管理平台WebVirtMgr部署-完整记录(2)
  15. linux bash的重定向
  16. web.config中连接字符串的读写和加密解密
  17. Android工具类-关于网络、状态的工具类
  18. 【C#】使用DWM实现无边框窗体阴影或全透窗体
  19. iOS 动画效果:Core Animation &amp; Facebook&#39;s pop
  20. 【转发】【小程序】微信小程序日常开发中常遇到的错误代码

热门文章

  1. 如何通过JS调用某段SQL语句
  2. 项目自动化建构工具gradle 入门4——javaWeb在浏览器中显示helloWorld
  3. 前端之jquery
  4. Linux Posix线程条件变量
  5. html5 自定义标签取值
  6. asp.net获取服务器绝对路径和相对路径
  7. C#-WebForm-★内置对象简介★Request-获取请求对象、Response相应请求对象、Session全局变量(私有)、Cookie全局变量(私有)、Application全局公共变量、ViewState
  8. SQL Server 阻止了对组件 &#39;Ad Hoc Distributed Queries&#39; 的 STATEMENT&#39;OpenRowset/OpenDatasource&#39; 的访问
  9. linux单网卡多IP配置
  10. 让 FreeBSD 和 Gentoo Linux 在 ZFS 存储卷上共存