Power Strings----poj2406(kmp扩展 循环节)

题目链接:http://poj.org/problem?id=2406

题意:就是求串s能够最多由多少个相同的串a串联而成;

例如 ababab 由3个ab串联而成;

abababa 只能由1个abababa组成;

kmp中的Next[n](下标从0开始)表示n个元素的前缀和后缀的最大匹配;

  a b a b a b a b

 0 1 2 3 4 5 6 7

0-5和2-7是最大匹配Next【n】= 6;ab就是循环节(因为n/(n-next[n])是整数,所以是循环节)即 n - Next【n】;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

#define N 1000010

char s[N];
int Next[N], n;

void Getnext()
{
    int i = 0, j = -1;
    Next[0] = -1;
    while(i<n)
    {
        if(j==-1 || s[i] == s[j])
            Next[++i] = ++j;
        else
            j=Next[j];
    }
}

int main()
{
    while(scanf("%s", s),s[0]!=‘.‘)
    {
        n = strlen(s);

        Getnext();

        int ans = n-Next[n];

        if(n%ans!=0)
            ans = 1;
        else
            ans = n/ans;
        printf("%d\n", ans);
    }
    return 0;
}
/*
aabaabaa
1
*/

时间: 2024-08-09 03:53:24

Power Strings----poj2406(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 = "

poj 2406 Power Strings(kmp循环节)

题目链接:http://poj.org/problem?id=2406 题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]. 例如:      a    b    a    b    a    b next:-1   0    0    1    2    3    4 next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n]; 1 #include <iostream> 2

poj 2406 Power Strings(KMP求循环次数)

题目链接:http://poj.org/problem?id=2406 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 = "abcdef". If we think of concatenation as multiplication, e

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

「UVA10298」 Power Strings(KMP

题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 abcd aaaa ababab . 输出样例#1: 复制 1 4 3 题解 Luogu的题解 这里是对目前最高赞题解结论的证明. 结论:设字符串长度为$n$,最长相同前后缀的长度为$next[i]$, 如$n$%$(n-next[n])=0$,则答案为$n/(n-next[n])$,否则为$1$. 证明: 我们求$next$数组的时候,相当于每次把当前串这样对齐了一下↓ 而$next$求到$n$时,上面

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: 33389   Accepted: 13869 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 = "

Power Strings(KMP)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 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 = "