http://poj.org/problem?id=1961

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 18542   Accepted: 9007

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 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

Source

 
 
 next数组的应用,代码意会吧、、
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
int len,p[N];
char s[N]; inline void Get_next()
{
for(int j=,i=;i<=len;p[i++]=j)
{
for(;s[i]!=s[j+]&&j>;) j=p[j];
if(s[i]==s[j+]) j++;
}
} int main()
{
for(int i=;scanf("%d",&len)&&len;)
{
scanf("%s",s+);
memset(p,,sizeof(p));
Get_next();
printf("Test case #%d\n",++i);
for(int i=;i<=len;i++)
{
int l=i-p[i];
if(i!=l&&i%l==)
printf("%d %d\n",i,i/l);
}
printf("\n");
}
return ;
}

最新文章

  1. 【CentOS】学习Bash
  2. Java动态代理与Cglib库
  3. ionic的start-y属性初始化页面
  4. jqueryIFrame框架内元素操作
  5. System V 信号量
  6. easyui之datagrid(不定时补充)
  7. Inno setup卸载前退出进程、删除文件夹
  8. hdu 2454 Degree Sequence of Graph G (推断简单图)
  9. TCP的三次握手(建立连接)与 四次挥手(关闭连接)
  10. Locally Weighted Linear Regression 局部加权线性回归-R实现
  11. Markdown 编辑器语法 专题
  12. HTML中文本过长时自动隐藏末尾部分或中间等任意部分
  13. 部署高可用keepalived组件
  14. Python爬虫之PyQuery使用(六)
  15. Luogu 2173 [ZJOI2012]网络 - LCT
  16. java 路径、className.class.getResourceAsStream()、ClassLoader.getSystemResourceAsStream() 、FileInputStream
  17. zookeeper应用 - leader选举 锁
  18. Linux操作系统启动界面(字符or图形界面)的设置及切换方法
  19. 《转》python学习(8)元组
  20. C++切勿混用带符号类型和无符号类型

热门文章

  1. P3649 [APIO2014]回文串(回文树)
  2. leetcode笔记:Range Sum Query - Mutable
  3. NYOJ 541 最强的战斗力
  4. CIKM 2013 Paper Modeling interaction features for debate side clustering
  5. Invalid command &amp;#39;WSGIScriptAlias&amp;#39;, perhaps misspelled or defined by a module not included in the ser
  6. 鸟哥的Linux私房菜-----15、例行性命令at与crontab
  7. vue30-单一事件管理组件通信: vuex
  8. 15.C语言多线程实现变色龙以及cmd窗口标题变化
  9. CloudFoundry 云平台部署
  10. C++中冒号(:)的作用