KMP + 求最小循环节 --- HDU 1358 Period
来源:互联网 发布:淘宝手机端首页装修 编辑:程序博客网 时间:2024/05/17 23:16
Period
Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358
Mean:
给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期。
analyse:
还是next数组的运用,详见上一篇博客。
Time complexity: O(N)
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-07.12
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN=1000010;
int n;
char s[MAXN];
int Next[MAXN];
void getNext()
{
Next[0]=0;
for(int i=1,k=0;i<n;++i)
{
while(s[i]!=s[k]&&k) k=Next[k-1];
if(s[i]==s[k]) ++k;
Next[i]=k;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int cas=1;
while(~scanf("%d",&n)&&n)
{
scanf("%s",s);
getNext();
printf("Test case #%d\n",cas++);
for(int i=1;i<n;++i)
{
int now_cycle=(i+1)-Next[i];
if((now_cycle!=i+1) && (i+1)%now_cycle==0)
{
printf("%d %d\n",i+1,(i+1)/now_cycle);
}
}
puts("");
}
return 0;
}
/*
*/
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-07.12
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN=1000010;
int n;
char s[MAXN];
int Next[MAXN];
void getNext()
{
Next[0]=0;
for(int i=1,k=0;i<n;++i)
{
while(s[i]!=s[k]&&k) k=Next[k-1];
if(s[i]==s[k]) ++k;
Next[i]=k;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int cas=1;
while(~scanf("%d",&n)&&n)
{
scanf("%s",s);
getNext();
printf("Test case #%d\n",cas++);
for(int i=1;i<n;++i)
{
int now_cycle=(i+1)-Next[i];
if((now_cycle!=i+1) && (i+1)%now_cycle==0)
{
printf("%d %d\n",i+1,(i+1)/now_cycle);
}
}
puts("");
}
return 0;
}
/*
*/
0 0
- KMP + 求最小循环节 --- HDU 1358 Period
- hdu 1358 period 求循环节 KMP
- HDU-1358 Period(KMP求循环节)
- [KMP求最小循环节][HDU1358][Period]
- HDU 1358 Period (next跳表找最小循环节)KMP
- HDU 1358 Period (kmp求循环节)
- Period--KMP,最小循环节
- UVALive 3026 Period ( kmp 求前缀最小循环节)
- poj1961--Period(KMP求最小循环节)
- (POJ1961)Period <KMP算法求最小循环节>
- POJ 1961 Period——kmp求最小循环节
- [KMP-循环节问题]HDU 1358 period
- hdu 1358 Period(KMP 循环节)
- HDU 1358 Period(kmp循环节)
- hdu 1358 Period(KMP循环节)
- HDU 1358 Period [KMP+循环节]
- HDU 1358 Period(KMP+求某个前缀含几个循环)
- hdu 1358 Period(KMP循环)
- Greedy --- HNU 13320 Please, go first
- 使用GitHub建立个人网站
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
- 菜鸟vimer成长记——第2.1章、normal模式
- 菜鸟vimer成长记——第2.2章、visual模式
- KMP + 求最小循环节 --- HDU 1358 Period
- KMP + 求最小循环节 --- HUST 1010
- KMP + 求最小循环节 --- POJ 2406 Power Strings
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
- “练习一万小时”定律
- Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
- 2015 Multi-University Training Contest 3 1002 RGCDQ
- 2015 Multi-University Training Contest 3 1001 Magician
- view、surfaceview、GLsurfaceview的区别和应用场景