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加深了理解 */ /************************************** ...
随机推荐
- c++容器(vector、list、deque)
vector ,deque 和 list 顺序性容器: 向量 vector : 是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩展.它可以像数组一样被操作,由于它的特性我们完全可 ...
- 【C++】第1章 在VS2015中用C++编写控制台应用程序
分类:C++.VS2015 创建日期:2016-06-12 一.简介 看到不少人至今还在用VC 6.0开发工具学习C++,其实VC 6.0开发工具早就被淘汰了.这里仅介绍学习C++时推荐使用的两种开发 ...
- hibernate映射文件one-to-one
one-to-one 元素 属性: name:映射类属性的名字 class:映射的目标类 cascade:设置操作中的级联策略 可选值为 all所有操作情况均进行级联.none所有操作情况均不进行级联 ...
- [python学习笔记]Day3
函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...
- sphinx使用随笔
为什么需要进行全文搜索呢? 一个表中有a.b.c多个字段.我们使用sql进行like搜索的时候,往往只能匹配某个字段.或者是这样的形式:a LIKE “%关键词%”or b LIKE “关键词” 这样 ...
- [性能] Bean拷贝工具类性能比较
Bean拷贝工具类性能比较 引言 几年前做过一个项目,接入新的api接口.为了和api实现解耦,决定将api返回的实体类在本地也建一个.这样做有两个好处 可以在api变更字段的时候保持应用稳定性 可以 ...
- javascript Array 方法学习
原生对象Array学习 Array.from() 从类似数组的对象或可迭代的对象返回一个数组 参数列表 arraylike 类似数组的对象或者可以迭代的对象 mapfn(可选) 对对象遍历映 ...
- Vue基础理论
一 vue的定位 (1)Vue.js是一个构建数据驱动的 web 界面的库. (2)Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. (3)Vue.js 自身不是一 ...
- 【读书笔记】iOS网络-运行循环
运行循环是由类NSRunLoop表示的,有些线程可以让操作系统唤醒睡眠的线程以管理到来的事件,而运行循环则是这些线程的基本组件.运行循环是这样一种循环,可以在一个周期内调度任务并处理到来的事件.iOS ...
- IOS 简单动画 首尾式动画
首尾式动画 首尾式动画即通过实现控件由初始状态到结束状态的过程.(主要表现在控件的Frame 透明度 ) // // ViewController.m // CX 简单动画 // // Created ...