(简单) POJ 1961 Period,扩展KMP。
Description
题目就是给一个字符串,然后让对从2到N的所有前缀字符串,找出里面的重复串。
对于找重复串的问题,用扩展KMP就可以解决。然后就是对于每个前缀,可以证明后面的前缀的最小重复串的长度一定大于等于前面的,因为形成重复串的必要条件就是i+next1[i]>=length 这样的话如果前面不符合这个式子,后面就更不用说了,必须让i增大才可能。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月18日 星期六 10时01分22秒
// File Name : 1961.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; void EKMP_pre(int m,char s[],int next1[])
{
int p=,a=,L; next1[]=m; while(p+<m && s[p]==s[p+])
++p; next1[]=p; for(int k=;k<m;++k)
{
L=next1[k-a];
p=next1[a]+a-(next1[a]!=); if(k+L-<p)
next1[k]=L;
else
{
++p; while(p<m && s[p]==s[p-k])
++p; next1[k]=p-k;
a=k;
}
} next1[m]=;
} int next1[MaxN];
char s[MaxN];
int N; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int cas=;
int minn; while(~scanf("%d",&N) && N)
{
minn=;
scanf("%s",s); EKMP_pre(N,s,next1); printf("Test case #%d\n",cas++); for(int i=;i<N;++i)
{
while(minn<=i && next1[minn]+minn<i+)
++minn; if(minn<=i && (i-minn+)%minn==)
printf("%d %d\n",i+,(i-minn+)/minn+);
} puts("");
} return ;
}
(简单) POJ 1961 Period,扩展KMP。的更多相关文章
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- poj 1961 Period(KMP训练指南例题)
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 11356 Accepted: 5279 Descripti ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- POJ 1961 2406 (KMP,最小循环节,循环周期)
关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406 Powe ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- POJ 1961 Period KMP算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
随机推荐
- 获取表空间的语句 以及 建表和索引的ddl
alter session set container=PHD1; SET SERVEROUTPUT ON SET LINESIZE SET FEEDBACK OFF SET PAGESIZE sel ...
- 【多重背包模板】poj 1014
#include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...
- GDB: basics
Before Debugging, generating the debugging info using gcc -g3 *.c/cpp; then gdb ~.out/exe using comm ...
- [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888
[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888 标签: androidbitmapjni 2014-05-09 20:35 2985人阅读 评论(1) 收 ...
- hdu 4463 Outlets(最小生成树)
Outlets Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submi ...
- FlashDevelop调试Air出错
按下F5调试命令行窗口出现错误: Starting AIR Debug Launcher... invalid application descriptor: Unknown namespace: h ...
- material design 的android开源代码整理
material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...
- time_wait和clost_wait说明
在服务器的日常维护过程中,会经常用到下面的命令: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 它会显示例如 ...
- kill -QUIT <pid>
On Solaris and Linux a thread dump is also printed if the J2SE process receives a QUIT signal. So ki ...
- POj-3104 Drying 二分+贪心
题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 ...