Period

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

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
 题解:看了半天题意,,题意:给定一个字符串,问这个字符串的所有前缀中,有哪些前缀可以由某个串重复k次组成,
   这个k需要大于1。于是我们可以想到,next【i】的值就是长度,当i%next[i]==0是开始匹配i/next[i]便是重复度k;
 
用了strlen就超时了。。。看来题目中给的N很有必要啊;
代码贴上:
 #include<stdio.h>
const int MAXN=;
int N;
char m[MAXN];
int next[MAXN];
void getnext(){
int i=,j=-;
next[i]=j;
while(i<N){
if(j==-||m[i]==m[j]){
i++;j++;
next[i]=j;
}
else j=next[j];
}
}
void print(int x){
for(int i=;i<=x;i++){
if(next[i]!=&&i%(i-next[i])==)printf("%d %d\n",i,i/(i-next[i]));
}
}
int main(){
int flot=;
while(~scanf("%d",&N),N){
scanf("%s",m);
getnext();
//for(int i=0;i<=N;i++)printf("%d ",next[i]);puts("");
//if(flot)puts("");
printf("Test case #%d\n",++flot);
print(N);
puts("");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=;
int p[MAXN];
char s[MAXN];
int N;
void getp(){
int i=,j=-;
p[]=-;
while(i<N){
if(j==-||s[i]==s[j]){
i++;j++;p[i]=j;
if(s[i]!=-&&i%(i-p[i])==&&i/(i-p[i])>){
printf("%d %d\n",i,i/(i-p[i]));
}
}
else j=p[j];
}
}
/*
void kmp(int& ans){
getp();
int j=0,i=0;
while(i<N){
if(j==-1||s[j]==m[i]){
i++;j++;
if(j==M){
ans=i-j+1;
return ;
}
}
else j=p[j];
}
}
*/
int main(){
int kase=;
while(~scanf("%d",&N),N){
scanf("%s",s);
printf("Test case #%d\n",++kase);
getp();
puts("");
}
return ;
}
 

Period(kmp)的更多相关文章

  1. HDU - 1358 - Period (KMP)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. POJ 1961 Period(KMP)

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

  3. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  4. HDU 1358 Period(kmp简单解决)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  6. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  7. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

  8. 经典串匹配算法(KMP)解析

    一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...

  9. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

随机推荐

  1. 多线程并发 synchronized对象锁的控制与优化

    本文针对用户取款时多线程并发情境,进行相关多线程控制与优化的描述. 首先建立用户类UserTest.业务操作类SynchronizedTest.数据存取类DataStore,多线程测试类MultiTh ...

  2. PCRE-正则库及用法

    摘自http://blog.chinaunix.net/uid-26575352-id-3517146.html    在C语言中利用PCRE实现正则表达式 http://www.pcre.org/ ...

  3. 总结FormsAuthentication的使用

    一.先看一下使用FormsAuthentication做登录认证的用法 用法一: FormsAuthentication.SetAuthCookie(username, isPersistent); ...

  4. Unity 接MM横屏闪退的原因

    =.=研究了1天接SDK到处都在报错,于是使用logcat查看原因截取到这样的Exception. call to OpenGL ES API withno current context(logge ...

  5. Vanya and Scales(思维)

    Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Java正則表達式

    近期工作中常常要用到正則表達式,不得不花点时间对其进行一定的学习.   JDK中提供了2个类来支持正則表達式,各自是java.util.regex.Pattern和java.util.regex.Ma ...

  7. 1. 用U盘安装Centos6.5 + Win7 双系统

    一. 用U盘安装Centos6.5 + Win7 双系统 准备工作:U盘(8G).需要安装的Centos6.5系统(64bit).EasyBCD(用来修复引导,否则开机只有一个系统).         ...

  8. 使用Jquery UI 高仿百度搜索下拉列表功能

    最近项目有个需求,在新添加商户的时候,用户输入商户名称后,如果系统中有类似的商户名称,直接显示出来,如下图的效果: 实现这个功能,直接使用了JQuery UI 插件 目前我使用的实现版本是: 网友可以 ...

  9. 转:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  10. c# 面相对象3-之继承性

    继承:类与类之间的关系(父子关系) 子类继承父类,那么子类就拥有父类的公共属性和方法一个子类只能继承一个父类,一个父类可以有好多子类子类对象可以直接转成父类,但父类只能转回对应的子类如果子类转成父类之 ...