首页 > 其他 > 详细

HDU 1358 Period

时间:2014-07-22 22:51:24      阅读:350      评论:0      收藏:0      [点我收藏+]

经典kmp

 ps:poj 2046 Power Strings 是这题的简化版 ←_←

太水就不贴代码了。。。

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn=1000010;

char s[maxn];
int next[maxn];
int n;

void getnext (char* s,int* next){
    next[0]=next[1]=0;
    for (int i=1;i<n;i++){
        int j=next[i];
        while (j&&s[i]!=s[j])
            j=next[j];
        next[i+1]=s[i]==s[j]?j+1:0;
    }
}

int main (){
    int kase=0;
    while (~scanf ("%d",&n)&&n){
        scanf ("%s",s);
        getnext (s,next);
        cout<<"Test case #"<<++kase<<endl;
        for (int i=1;i<=n;i++){
            if (next[i]&&i%(i-next[i])==0)
                cout<<i<<" "<<i/(i-next[i])<<endl;
        }
        cout<<endl;
    }
    return 0;
}

HDU 1358 Period,布布扣,bubuko.com

HDU 1358 Period

原文:http://www.cnblogs.com/gfc-g/p/3855234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!