Period

Time Limit: 3000MS Memory Limit: 30000K

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


  • 题意就是给你一个字符串,问分别以字符串中的每一个字符结尾是否可以形成循环节,可以形成多少个循环节。

  • 其实就是考察的一个next数组。


#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e6+100;
char s[maxn];
int next[maxn]; void cal_next()
{
int k;
next[0] = k = -1;
int len = strlen(s);
for(int i=1;i<len;i++)
{
while(k>-1 && s[i]!=s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
} void solve()
{
int len = strlen(s);
for(int i=1;i<len;i++)
{
if(next[i] == -1)
continue;
if((i+1)%(i + 1 - next[i] - 1) == 0)//形成循环节是一定没有余数的情况下
printf("%d %d\n",i+1,(i+1)/(i+1-next[i]-1));
}
} int main()
{
int n,t = 1;
while(scanf("%d",&n) && n)
{
scanf("%s",s);
printf("Test case #%d\n",t++);
cal_next();
solve();
printf("\n");
}
return 0;
}

POJ:1961-Period(寻找字符串循环节)的更多相关文章

  1. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  2. poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Descripti ...

  3. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

  4. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...

  5. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  6. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  7. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  8. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  9. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  10. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

随机推荐

  1. js解析url参数

    1.采用正则,这也是现在使用最为方便的 function getQueryString(name) { const reg = new RegExp("(^|&)" + n ...

  2. LCD1602显示中文汉字

    小子在西藏 2011-11-25编写 特别说明笔者是上面的作者,感谢那些原意分享知识的人.时隔5年我又看到了笔者当年写的东西,我想这期间还有许许多多的人 今天写在博客上,愿更多后来者可以学习. LCD ...

  3. Webstorm 激活

    注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key.php 点击:Acti ...

  4. EF 记录执行的sql语句

    最近做了个中等的项目,数据不会很多,开发时间比较紧迫,所以用了EF的框架. 在使用过程中,发现有时候执行的结果不如预期,想看看执行的sql语句为何,遍查找资料,在网上找到了相关辅助类,拿来使用,部署到 ...

  5. BroadCast广播机制应用与实例

    如何编写广播接收器 第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法. class MyBroadcastReceiver extends Broadcast ...

  6. ”position”之绝对定位深入理解

    要点: 1.绝对元素脱离文档流 它从文档流中脱离了出来,后面的元素会填充掉它原来的位置 2.绝对定位元素定位 以离他最近的.有定位的.祖先元素 为准 参照对象决定元素的位置 情况1 <div ( ...

  7. Android 贝塞尔曲线的浅析

    博客也开了挺长时间了,一直都没有来写博客,主要原因是自己懒---此篇博客算是给2017年一个好的开始,同时也给2016年画上一个句点,不留遗憾. 那就让我们正式进入今天的主题:贝塞尔曲线. 首先,让我 ...

  8. Python+selenium之测试报告(1)

    一.下载HTMLTestRunner.py HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展.它生成易于使用的 HTML 测试报告.HTMLTestRunne ...

  9. Python+selenium之疑难点解决之去除readonly的限制

    去除文本框的readonly只读模式的限制 如图所示:去除卡号readonly限制 代码如图所示: js = 'document.getElementById("cardNo"). ...

  10. 协议详解3——IP

    1. 特点: 所有的TCP,UDP,ICMP,IGMP数据都以IP数据报格式传输.  提供不可靠,无连接服务. 不可靠: 不能保证IP数据报能成功到达目的.IP仅提供最好的传输服务.如果发生某种错误时 ...