Power Strings

Time Limit: 3000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

题意:求解最多重复子串
利用KMP的前缀数组 以p为模式串 next【i】的意思 为前个字符组成的子串为s 则s的前next【i】个字符与后next【i】个字符相等
注意 : len(p)-next【len(p))】==循环节的长度
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
int next[];
char p[]; void find(char p[])
{
int m=strlen(p+);
next[]=;
for(int k=,q=; q<=m; q++)
{
while(k>&&p[k+]!=p[q])
k=next[k];
if(p[k+]==p[q])
k++;
next[q]=k;
}
} int main()
{ while(~scanf("%s",p+))
{
if(!strcmp(".",p+))
break;
find(p);
int len=strlen(p+);
int len1=len-next[len];
printf("%d\n",len%len1?:len/len1);
} }

Period

Time Limit: 3000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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.

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 题意: 定义字符串A,若A最多由n个相同字串s连接而成,则A=s^n,如"aaa" = "a"^3,"abab" = "ab"^2 "ababa" = "ababa"^1 给出一个字符串A,求该字符串的所有前缀中有多少个前缀SA= s^n(n>1) 输出符合条件的前缀长度及其对应的n
如aaa 前缀aa的长度为2,由2个'a'组成 前缀aaa的长度为3,由3个"a"组成
分析:KMP
若某一长度L的前缀符合上诉条件,则
1.next[L]!=0(next[L]=0时字串为原串,不符合条件)
2.L%(L-next[L])==0(此时字串的长度为L/next[L]) 对于2:有str[0]....str[next[L]-1]=str[L-next[L]-1]...str[L-1]
=》str[L-next[L]-1] = str[L-next[L]-1+L-next[L]-1] = str[2*(L-next[L]-1)];
假设S = L-next[L]-1;则有str[0]=str[s]=str[2*s]=str[3*s]...str[k*s],对于所有i%s==0,均有s[i]=s[0]
同理,str[1]=str[s+1]=str[2*s+1]....
str[j]=str[s+j]=str[2*s+j]....
综上,若L%S==0,则可得L为str[0]...str[s-1]的相同字串组成,
总长度为L,其中字串长度SL = s-0+1=L-next[L],循环次数为L/SL
故对于所有大于1的前缀,只要其符合上述条件,即为答案之一
#include "stdio.h"
int p[],N;
char str[]; void get_p(int n)
{
int i,j=-;
p[]=-;
for(i=;i<n;i++)
{
while(j>- && str[i]!=str[j+]) j=p[j];
if(str[i] == str[j+]) j++;
p[i]=j;
}
} int main()
{
int i,j,cas=;
while(scanf("%d",&N),N)
{
scanf("%s",str);
get_p(N);
printf("Test case #%d\n",cas++);
for(i=;i<N;i++)
{
if(p[i]!=- && (i+)%(i-p[i])==)
printf("%d %d\n",i+,(i+)/(i-p[i]));
}
printf("\n");
}
}

http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html  看一看


poj 2046&&poj1961KMP 前缀数组的更多相关文章

  1. poj 2566Bound Found(前缀和,尺取法)

    http://poj.org/problem?id=2566: Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  2. codeforces 381 D Alyona and a tree(倍增)(前缀数组)

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. 转载-KMP算法前缀数组优雅实现

    转自:http://www.cnblogs.com/10jschen/archive/2012/08/21/2648451.html 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见 ...

  4. 子串查询(二维前缀数组) 2018"百度之星"程序设计大赛 - 资格赛

    子串查询 Time Limit: 3500/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  5. 线段树、前缀数组:HDU1591-Color the ball(区间更新、简单题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. 前缀数组O(n^3)做法

    前缀数组O(n^3)做法 s.substr()的应用非常方便 令string s = "; ); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789&quo ...

  7. POJ-2752(KMP算法+前缀数组的应用)

    Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...

  8. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

  9. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

随机推荐

  1. java mina框架使用

    1.目前为止,看到写mina最清晰的一篇博客:https://my.oschina.net/ielts0909/blog/85946! 2.官网的开发文档:http://mina.apache.org ...

  2. HapMap

    HapMap五周年回顾 2011-01-12 | 作者: [关闭] 作者简介:曾长青,中国科学院北京基因组所研究员,博士生导师.CUSBEA奖学金.百人计划.杰出青年基金.首批新世纪百千万人才工程国家 ...

  3. idea 高级调试技巧

    两年前写过一篇关于idea的高级用法,今天再来一篇关于调试方面的技巧讲解: 一.条件断点 循环中经常用到这个技巧,比如:遍历1个大List的过程中,想让断点停在某个特定值. 参考上图,在断点的位置,右 ...

  4. 担心后端代码泄露?用delphi做后端,模板扣出来,随时可以变化。

    担心后端代码泄露?用delphi做后端,模板扣出来,随时可以变化. 本项目不是intraweb, unigui等类似的拖拉项目,只是一个简单 的模板引擎,理论上可以结合任何后端. 要就下载源码,作者保 ...

  5. @1-2初识Python爬虫

    初识Python爬虫 Python爬虫(入门+进阶)     DC学院 环境搭建: Python2与Python3的差异:python2与python3整体差异不大,大多是一些语法上的区别,考虑到py ...

  6. .net利用NPOI生成excel文件

    整理代码,这个是生成excel文件,用的是HSSF的方式,只能生成65535行,256列的数据,如果要看office07之后的生成,之前的随笔里提过.这个是一个完整的过程. 首先是已经查找好的数据,这 ...

  7. 【C#】解析C#程序集的加载和反射

    目录结构: contents structure [+] 程序集 程序集的加载 发现程序集中的类型 反射对类型成员的常规操作 发现类型的成员 创建类型的实例 绑定句柄减少进程的内存消耗 解析自定义特性 ...

  8. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  9. python早期看书笔记

  10. 谷歌开源OCR,tesseract-ocr使用笔记

    官方教程地址:https://github.com/tesseract-ocr/tesseract/wiki/Compiling 测试版本为 root@9a2a063f9534:/tesseract/ ...