码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1358 Period

时间:2014-07-22 22:52:16      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   

经典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

标签:style   blog   color   os   io   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!