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. Minimum Depth of Binary Tree 解答

    Question Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along ...

  2. GridBagLayout占多行效果注意

    如果想要出现按钮2占两行的效果,必须按键3.按钮4同时存在且同时可见. 如果缺少按钮4,则按钮2不会占两行: 如果缺少按钮3.4,则按钮2也不会占两行. package com.wst.bj; imp ...

  3. asp.net 错误处理

    一.从客户端(...)中检测到有潜在危险的 Request.Form 值.(如图) 解决办法: 1.为 c:/windows/temp 文件夹 设置 IIS_Iusers 可读写权限 (可解决部分问题 ...

  4. 总结一下用caffe跑图片数据的研究流程

    近期在用caffe玩一些数据集,这些数据集是从淘宝爬下来的图片.主要是想研究一下对女性衣服的分类. 以下是一些详细的操作流程,这里总结一下. 1 爬取数据.写爬虫从淘宝爬取自己须要的数据. 2 数据预 ...

  5. 登录DSCCC控制台报错提示:安装错误代码: 3

    登录DSCCC控制台报错内容:读取安装配置时出错 检查目录服务控制中心状态时出现意外错误. 显示详细资料 隐藏详细资料 安装错误代码: 3 堆栈: com.sun.directory.common.s ...

  6. putty保持连接不自动段开

    经常在网上看到有人说自己利用putty工具登录服务器总是连接不上,这样的情况自己在刚接触putty时也遇到过.在 Connection 里面有个 Seconds between keepaliaves ...

  7. .net 链接oracle

    虽然EF6都快要出来了,但是对于Oracle数据库,仍然只能用DB first和Model First来编程,不能用Code First真是一个很大的遗憾啊. 好了,废话少说,我们来看看EF中是如何用 ...

  8. 自定义view(自定义view的时候,三个构造函数各自的作用)

    package com.timeshare.tmband.Utils; import android.content.Context; import android.content.res.Typed ...

  9. 为 IIS 7.0 配置 <system.webServer>

    Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置.system.WebServer 是 configuration 节的子级 ...

  10. 加密html

    2009年4月4日 周六 22:18 <HTML> <HEAD> <meta http-equiv="Content-Type" content=&q ...