POJ2406 Power Strings【KMP】

题目链接:

http://poj.org/problem?id=2406

题目大意:

给定两个字符串a和b,定义a*b为两个字符串的链接。比如,a = "abc",b = "def",则

a*b ="abcdef"。这个定义当作是多项式。则一个字符串的非负整数次幂可定义如下:

a^0 = "",a^(n+1) = a*a^n。

现在给你一个字符串s,求出最大的n,满足s = a^n(a为s的某个子串)。比如s = "aaaa",

则n最大为4,a = "a",s = "a"^4。

思路:

对于给定的字符串s,最短的重复子串a是s[Next[len]] s[Next[len+1]] … s[len-1]。当

len % Next[len] = 0时,说明字符串s有不是它本身重复子串a,最大的n为len/Next[len]。

当len % Next[len] != 0时,除了s,没有重复子串a满足要求,这时n = 1。

AC代码:

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

char str[1000010];
int Next[1000010],len;

void GetNext()
{
    int i = 0,j = -1;
    Next[0] = -1;
    while(i <= len)
    {
        if(j == -1 ||  str[i]==str[j])
        {
            i++,j++;
            Next[i] = j;
        }
        else
            j = Next[j];
    }
}
int main()
{
    while(cin >> str && strcmp(str,".")!=0)
    {
        len = strlen(str);
        GetNext();
        if(len % (len-Next[len]) == 0)
            cout << len/(len-Next[len]) << endl;
        else
            cout << 1 << endl;
    }

    return 0;
}
时间: 2024-09-29 22:40:13

POJ2406 Power Strings【KMP】的相关文章

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

PKU 2406:Power Strings 【KMP】

Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 70   Accepted Submission(s) : 27 Problem Description Given two strings a and b we define a*b to be their concatenation. For example,

poj 2406 Power Strings【KMP】

点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b =

poj 2406 Power Strings 【kmp】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37564   Accepted: 15532 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

poj 2406 Power Strings 【KMP的应用】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33595   Accepted: 13956 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

poj2406 Power Strings(kmp失配函数)

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcd

poj2406 Power Strings (KMP)

这题跟HDU 1358 Period (KMP)差不多,稍微修改代码就行了. 关于KMP的更多知识,请关注从头到尾彻底理解KMP(2014年8月4日版). #include<stdio.h> #include<string.h> int n,next[1000000]; char p[1000000]; void getnext() { int k=0,j=1; next[0]=-1;next[1]=0; while (j<n) { if (k==-1||p[j]==p[k]

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

HDU2594 Simpsons’ Hidden Talents 【KMP】

Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 1055 Problem Description Homer: Marge, I just figured out a way to discover some of the