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

POJ 2406 Power Strings KMP

时间:2020-12-01 12:21:17      阅读:7      评论:0      收藏:0      [点我收藏+]

标签:sys   pac   cst   void   ace   poj   string   name   ext   

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

int len;

void getNext(char *s, int *nex)
{
    nex[0] = -1;
    int i = 0, j = -1;
    while(i < len)
    {
        if(j == -1 || s[i] == s[j])
        {
            i++, j++;
            nex[i] = j;
        }
        else j = nex[j];
    }
}

int main()
{
    char s[1000100];
    while(~scanf("%s", s))
    {
        getchar();
        if(s[0] == ‘.‘) break;
        //cout << s << endl;
        len = strlen(s);
        int nex[1000100];
        getNext(s, nex);
        int a = nex[len];
        if(a*2 < len) printf("1\n");
        else
        {
            if(len%(len-a) == 0)  printf("%d\n", len/(len-a));
            else printf("1\n");
        }
    }
    //system("pause");
    return 0;
}

POJ 2406 Power Strings KMP

标签:sys   pac   cst   void   ace   poj   string   name   ext   

原文地址:https://www.cnblogs.com/znk97/p/14043961.html

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