HDU1358--KMP找周期(KMP基本题型到此总结完毕)
来源:互联网 发布:网络上赌博怎么处罚 编辑:程序博客网 时间:2024/06/05 17:20
/**********************
* author:crazy_石头
* Pro:HDOJ 1358-Period
* algorithm: KMP
* Judge Status:Accepted
* Memory:5120K
* Time:78ms
* date:2013/11/01
***********************/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
const int maxn=1000000+5;
int next[maxn];
char p[maxn];
int n,test;//主串的串长;
inline void getNext(char *p,int *next)
{
int i,j;
i=0,j=-1;
next[0]=-1;
int m=strlen(p);
while(i<m)
{
if(j==-1||p[i]==p[j])
i++,j++,next[i]=j;
else
j=next[j];
}
}
int main()
{
test=1;
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%s",p);
printf("Test case #%d\n",test++);
getNext(p,next);
rep(i,2,n)
{
if(i%(i-next[i])==0&&next[i]>0)
printf("%d %d\n",i,i/(i-next[i]));
}
printf("\n");
}
return 0;
}
* author:crazy_石头
* Pro:HDOJ 1358-Period
* algorithm: KMP
* Judge Status:Accepted
* Memory:5120K
* Time:78ms
* date:2013/11/01
***********************/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
const int maxn=1000000+5;
int next[maxn];
char p[maxn];
int n,test;//主串的串长;
inline void getNext(char *p,int *next)
{
int i,j;
i=0,j=-1;
next[0]=-1;
int m=strlen(p);
while(i<m)
{
if(j==-1||p[i]==p[j])
i++,j++,next[i]=j;
else
j=next[j];
}
}
int main()
{
test=1;
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%s",p);
printf("Test case #%d\n",test++);
getNext(p,next);
rep(i,2,n)
{
if(i%(i-next[i])==0&&next[i]>0)
printf("%d %d\n",i,i/(i-next[i]));
}
printf("\n");
}
return 0;
}
* This source code was highlighted byYcdoiT. ( style: Pyte )
注意求next值得时候模式串的串长应该先算出来用一个变量m(或者其他的变量)承接一下,以后用这个变量,不然会T。。。。T_T
- HDU1358--KMP找周期(KMP基本题型到此总结完毕)
- hdu1358 Period(kmp周期)
- HDU1358 字符串求周期 KMP算法
- hdu1358 KMP
- KMP HDU1358
- hdu1358 kmp
- HDU1358-kmp
- HDU1358 Period(KMP找循环前缀)
- HDU1358 Period(KMP,找循环节)
- hdu1358 Period KMP之next函数灵魂 KMP的周期 周期 周期
- Period hdu1358 KMP
- hdu1358 period ,Kmp
- hdu1358 Period kmp+模拟
- hdu1358 Period(kmp)
- HDU1358 Period【KMP】
- hdu1358 Period--KMP
- hdu1358 KMP循环节
- HDU1358 Period(KMP)
- (使用STL自带的排序函数进行排序7.3.6)POJ 2945 Find the Clones拷贝相同次数的文本出现了多少次()
- leetcode Palindrome Partitioning I II
- 监控视频中人的特征识别
- DLNA
- VC编程 设置IE浏览器代理(不需重启浏览器)
- HDU1358--KMP找周期(KMP基本题型到此总结完毕)
- xxx is not in the sudoers file解决方法
- 补全MyReview database中的zmax_text table的方法(解决Zmax_Exception问题)
- EF版本变化及一些重要变更
- erlang android 通讯
- Jorm实现简单的数据库CRUD操作
- Java学习第三课 面向对象编程
- 程序设计思考
- Install WAS6.1+IHS for Linux(64bit)