POJ 2406 Power Strings (KMP)
Power Strings
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 29663
Accepted: 12387
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真心不容易,理解起来很吃力,还好今天老师有讲解KMP的基本原理,现在思路清晰了一点,赶紧做这道以前暴力过的题。
证明:(PKU 2406 POWER STRINGS --- 字符串匹配,KMP算法)
定理:假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]
例子证明:
设S=q1q2q3q4q5q6q7q8,并设next[8] = 6,此时str = S[len - next[len]] = q1q2,由字符串特征向量next的定义可知,q1q2q3q4q5q6 = q3q4q5q6q7q8,即有q1q2=q3q4,q3q4=q5q6,q5q6=q7q8,即q1q2为循环子串,且易知为最短循环子串。由以上过程可知,若len可以被len - next[len]整除,则S存在循环子串,否则不存在。
解法:利用KMP算法,求字符串的特征向量next,若len可以被len - next[len]整除,则最大循环次数n为len/(len - next[len]),否则为1。
1: #include <cstdio>
2: #include <cstring>
3: #include <algorithm>
4: using namespace std;
5: const int maxn=1e6;
6: char s[maxn+10];
7: int next[maxn+10];
8:
9: void get_next(char s[],int len)
10: {
11: int i=0,j=-1;
12: next[0]=-1;
13: while(i<len)
14: {
15: if(j==-1||s[i]==s[j]) {j++; i++; next[i]=j;}
16: else j=next[j];
17: }
18: }
19:
20: int main()
21: {
22: while(scanf("%s",s),s[0]!='.')
23: {
24: int len=strlen(s);
25: get_next(s,len);
26: if(len%(len-next[len])==0)
27: printf("%d\n",len/(len-next[len]));
28: else
29: printf("1\n");
30: }
31: return 0;
32: }
POJ 2406 Power Strings (KMP)的更多相关文章
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- poj 2406 Power Strings(KMP变形)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28102 Accepted: 11755 D ...
- POJ 2406 - Power Strings - [KMP求最小循环节]
题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...
- POJ 2406 Power Strings KMP求周期
传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...
- POJ 2406 Power Strings KMP运用题解
本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...
- POJ 2406 Power Strings KMP算法之next数组的应用
题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...
- poj 2406 Power Strings KMP匹配
对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...
- KMP POJ 2406 Power Strings
题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...
随机推荐
- 【Bootstrap基础学习】00 序
其实这样的东西很多了,但是我就是要写. 我写这种鬼东西只是为了监督自己,如果能顺便帮一下别人就更好了. 这个系列的基础学习,不会去看实体书,主要是去看网上的资料和官网. Bootstrap就是对jQu ...
- csharp: Speech
Speech SDK 5.1https://www.microsoft.com/en-us/download/details.aspx?id=10121 detects mobile devices ...
- Retention、Documented、Inherited三种注解
Retention注解 Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源 ...
- 回文串---Hotaru's problem
HDU 5371 Description Hotaru Ichijou recently is addicated to math problems. Now she is playing wit ...
- Docker源码编译
官方建议docker源码编译在docker容器内进行,因为官方提供的容器内已经继承了编译需要的环境,如果非要自己搭建编译环境也不是不可以,就是稍微有些繁琐.以下以1.8.2版本为例. 1.pull d ...
- Android系统兼容性问题(持续更新)
相信开发过一段Android的都被Android中的兼容性问题给折腾过,有时这确实很无奈,Android被不同的厂商改的七零八落的.本文主要总结下本人在实际的项目开发过程中所遇到的兼容性问题,以及最后 ...
- .NET Core的“dotnet restore”、“dotnet build”和“dotnet run”命令都是用来干什么的?
dotnet restore 源代码:https://github.com/dotnet/cli/tree/rel/1.0.0/src/dotnet/commands/dotnet-restore 入 ...
- JavaScripts 基础详细笔记整理
一.JS简介 JavaScript 是 Web 的编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理 ...
- NYOJ 21 三个水杯
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...
- jQuery eislideshow 图片轮播
在线实例 基础演示 自动播放 使用方法 <div id="ei-slider" class="ei-slider"> <ul class=&q ...