LA 3026(Period-MP算法)[Template:KMP]
来源:互联网 发布:梓潼淘宝运营招聘 编辑:程序博客网 时间:2024/06/05 10:03
3026 - Period
Time limit: 3.000 secondsFor each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 ≤ i ≤ N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 ≤ N ≤ 1 000 000) the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.
Output
For each test case, output 'Test case #' and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
本题用了MP算法
显然i能和f[i]匹配,那么字符串刚好可以连着等下去
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (1000000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}// kmpclass KMP{public:int f2[MAXN]; //字符串从0开始,但是f[i]表示匹配第i个字符,前面留一个 f[0]--a-->f[1]--...这样的 char T2[MAXN],P2[MAXN]; //T is long,P is model strvoid mem(){MEM(f2) MEM(T2) MEM(P2)}int getFail(char *P=0,int* f=0){if (P==0) P=P2;if (f==0) f=f2;int m=strlen(P);f[0]=f[1]=0; For(i,m-1){int j=f[i];while(j&&P[i]!=P[j]) j=f[j];f[i+1]= P[i] == P[j] ? j+1 : 0;}}int find(char* T=0,char* P=0,int* f=0){if (T==0) T=T2;if (P==0) P=P2;if (f==0) f=f2;int n=strlen(T),m=strlen(P);getFail(P,f);int j=0;Rep(i,n){while(j&&T[i]!=P[j]) j=f[j];if (T[i]==P[j]) j++;if (j==m) return i-m+1;}}}S;int n;int main(){//freopen("la3026.in","r",stdin);//freopen(".out","w",stdout);int tt=0;while(scanf("%d%s",&n,S.P2)==2){printf("Test case #%d\n",++tt);S.getFail(S.P2,S.f2);Fork(i,2,n){if (i%(i-S.f2[i])==0&&S.f2[i]) printf("%d %d\n",i,i/(i-S.f2[i])); } putchar('\n');S.mem();}return 0;}
- LA 3026(Period-MP算法)[Template:KMP]
- LA-3026 - Period-MP算法-失配函数-寻找循环节 (kmp)
- LA 3026 - Period KMP
- LA - 3026 - Period(KMP)
- LA-3026 Period(kmp应用)
- UVaLA 3026 Period MP算法
- LA 3026 Period (KMP失配函数)
- Period MP算法
- UVALive 3026 (LA 3026) Period KMP求字符串周期
- LA 3026 - Period KMP失配函数的应用
- KMP(MP)算法详解
- LA 3026 - Period
- LA 3026 Period
- LA 3026 Period
- UVa LA 3026 Period
- 【43.49】【LA 3026】Period
- Period(KMP算法)
- Period(KMP算法)
- layout
- /bin、/sbin、/usr/bin、/usr/sbin
- 马云台大演讲:这世界观点很多 请坚持自己思考
- 如何在Ubuntu上安装及简单配置SVN
- Windows客户端连接不上Linux服务器
- LA 3026(Period-MP算法)[Template:KMP]
- 开源Math.NET基础数学类库使用(08)C#进行数值积分
- pinyin4j把中文句子(含有多音字字母)转成拼音(二维数组递归求所有组合情况返回list)算法实现!
- 运算符之深度复制拷贝
- 安装Android Studio问题汇总
- 黑马程序员—继承与多态
- poj3311 全排列
- 用户空间应用程序如何加载模块到内核空间?
- Oracle数据泵