Poj-1961-Period-【KMP】
来源:互联网 发布:天津基础网络教研平台 编辑:程序博客网 时间:2024/05/22 07:05
传送门:http://poj.org/problem?id=1961
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 18517 Accepted: 8992
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.
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
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
Source
Southeastern Europe 2004
题意:
给你一个字符串,求这个字符串到第i个字符为止的循环节的次数。
比如aabaabaabaab,长度为12.到第二个a时,a出现2次,输出2.到第二个b时,aab出现了2次,输出2.到第三个b时,aab出现3次,输出3.到第四个b时,aab出现4次,输出4.
解题:
用KMP的next数组 题中用到怎样求循环节长度 就是i-next[i] 用i%(i-next[i])来判断是否有循环节
i/(i-next[i])求循环节个数
#include<cstdio>#include<cstring>char s[1000005];int next[1000005],n;void Next(){int i=0,j=-1;next[0]=-1;while(i<n){if(s[i]==s[j]||j==-1)next[++i]=++j;elsej=next[j];}}int main(){int t=1;while(scanf("%d",&n)!=EOF&&n){scanf("%s",s);//for(int i=0;i<n;i++)//这种输入会出错 //scanf("%c",&s[i]);Next();printf("Test case #%d\n",t++);for(int i=1;i<=n;i++){if(i%(i-next[i])==0&&i!=i-next[i])// i-next[i] 为循环节长度 整个if为判断有多少个循环 printf("%d %d\n",i,i/(i-next[i]));}printf("\n"); }return 0;}/*
阅读全文
0 0
- poj 1961:Period (KMP)
- poj 1961 Period (KMP)
- poj 1961 Period---kmp
- POJ 1961-Period:KMP
- poj 1961 Period KMP
- poj 1961 Period(KMP)
- Period - POJ 1961 KMP
- POJ 1961 Period (KMP)
- POJ Period 1961【KMP】
- POJ 1961 Period kmp
- poj 1961 Period 【KMP】
- POJ-1961(Period)(kmp())
- [poj 1961]Period[kmp]
- POJ 1961 Period (KMP)
- POJ 1961 Period(KMP)
- |poj 1961|KMP|Period
- Poj-1961-Period-【KMP】
- POJ 1961 Period (KMP)
- 几何板子
- VX Search Client 9.7.18 漏洞分析及利用
- 一个场景与另一个场景之间的通信
- Math类常用的方法
- Java基础之Java中的方法
- Poj-1961-Period-【KMP】
- 小希的迷宫------幷查集判断是否有环,是否是独立的一棵树
- Ubuntu14.04 CEPH 集群搭建(一)
- Android 获取控件的宽度和高度的几种方式
- Mybatis choose (when, otherwise)标签
- 网关和客户端交互
- C++ 读取ini文件
- handler机制的原理
- docker的基本概念