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. OpenWrt 学习网址

    http://m.blog.csdn.net/blog/woods2001/8137755

  2. iOS https认证 && SSL/TLS证书申请

    1.下面列出截止2016年底市面上常见的免费CA证书: 腾讯云SSL证书管理(赛门铁克TrustAsia DV SSL证书)阿里云云盾证书服务(赛门铁克DV SSL证书)百度云SSL证书服务Let's ...

  3. Linux-0.11内核源代码分析系列:内存管理get_free_page()函数分析

    Linux-0.11内存管理模块是源码中比較难以理解的部分,如今把笔者个人的理解发表 先发Linux-0.11内核内存管理get_free_page()函数分析 有时间再写其它函数或者文件的:) /* ...

  4. 将数组,表的某一列转换为string字符串的方法

    样例:字符串数组为array,str为字符串数组转换成的字符串 string[] array = { etr, kdgj, 3454, tyt, gff }; string str=string.Jo ...

  5. js下拉菜单默认值为当前年份,下拉菜单的选项为当前年份往前推5年

    1:在jsp或者HTML页面中新建一个下拉框 年度:<select id="cx_nd" name="cx_nd" style="width: ...

  6. 将整数数组按就分成2个部分,数组坐标为奇数,右边为偶数(java实现)

    方法1: 若不考虑性能: 使用一个新的数组target 遍历原数组 发现奇数则复制到target中 然后偶数 最后显示 import java.util.Arrays; public class Sp ...

  7. (zz)Lambda 表达式(C# 编程指南)

    https://msdn.microsoft.com/zh-cn/library/bb397687.aspx Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数.通过使用 lambd ...

  8. List小练习

    功能:创建链表节点,删除节点,顺序打印,不改变原结构的情况下分别用STL中的stack实现逆序打印和利用函数递归打印 代码如下: //链表问题struct ListNode {    int m_nV ...

  9. phpstorm8 配置svn

    步骤1 步骤2. 步骤3.

  10. weblogic启动时报错 java.lang.OutOfMemoryError: PermGen space

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域.这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入Perm ...