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. UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>

    E - 树上的距离 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Subm ...

  2. hdu1172猜数字(暴力枚举)

    猜数字 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  4. SQL*Plus break与compute的简单用法

    SQL*Plus break与compute的简单用法在SQL*Plus提示符下输出求和报表,我们可以借助break与compute两个命令来实现.这个两个命令简单易用,可满足日常需求,其实质也相当于 ...

  5. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  6. PHP.INI常用设置一览表(持续更新)

    在编程的过程中遇到或发现的问题,会持续的更新: 1. 打破var_dump的显示瓶颈 php开发环境里,安装了xdebug模块后,var_dump()输出的结果将比较易于查看,但默认情况下,var_d ...

  7. RabbitMQ消息队列安装和配置以及推送消息

    好久没有写了,最近项目用到RabbitMQ,找了一些资料试验,最后终于成功了,把安装配置的步骤分享给大家. 一.Erlang安装具体过程: 1.双击otp_win32_R16801.exe(不同版本可 ...

  8. RelativeLayout与LinearLayout的区别

    1.LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的 containers,超过边界时,某些控件将缺失或消失.因此一个垂 ...

  9. VMVare虚拟机的异常处理---内部错误

    由于昨天晚上关闭虚机时手快,直接把机器睡眠了.今天早上开机后,虚机显示启动状态,但是无法连接.搜了一下,网上说删掉.lk锁文件就可以了,删掉后,没有反应,强制关闭,再次启动VM,还是打不开,显示内部错 ...

  10. Fedora下用Iptux,中文乱码解决

    Ubuntu/Fedora下用Iptux与Windows下大飞鸽传书,中文乱码解决 问题描述: 在Ubuntu/Fedora下安装了Iptux后,再往Windows机器上发送文件或消息时,如果有中文, ...