POJ 1961 KMP

来源:互联网 发布:大学java专业入门课程 编辑:程序博客网 时间:2024/06/07 05:40

题目链接

题意: 求所有可以 表示成 (S)^K的前缀 输出前缀位置 和 k的最大值

代码:

#include <cstdio>#include <iostream>#include <vector>#include <cstring>#include <algorithm>#define sf scanf#define pf printfusing namespace std;const int maxn =  1000000 + 5;char str[maxn];int next[maxn];int getNext(char* p,int* next){    int m = strlen(p);    next[0] = 0;    next[1] = 0;    for(int i = 1;i < m;++i){        int j = next[i];        while(j && p[i] != p[j]){            j = next[j];        }        next[i + 1] = p[i] == p[j] ? j + 1 : 0;    }    return 0;}int main(){    int n,ca = 0;    while( sf("%d",&n) && n ){        sf("%s",str);getNext(str,next);        int ans = 0;        pf("Test case #%d\n",++ca);        for(int i = 1;i <= n;++i){            if(next[i] && i % (i - next[i]) == 0){                pf("%d %d\n",i,i / (i - next[i]) );            }        }        pf("\n");    }    return 0;}
0 0
原创粉丝点击