POJ2406 Power Strings next数组应用

题目描述:

给定一个字符串,求其最大循环次数(即求最小循环节长度)

输入样例

abcd

ababab

aaaa

.

输出样例

1

3

4

解题思路:

KMP算法中next数组的应用。

len-next[len]表示的是字符串相同前缀空出来的一段,由next数组性质可知,这一段可以不断向前推出相等,所以只要判断len是否可以整除len-next[len]就可以了。否则就是1.

代码如下

#include <cstdio>
#include <cstring>
const int maxn = 1000010;
int next[maxn];
char s[maxn];
void GetNext(char* p,int next[])
{
    int pLen = strlen(p);
    int k = -1;//k记录的是next[j]
    next[0] = k;
    int j = 0;
    while (j < pLen) {
        /** next[j]=-1时,next[j+1]肯定是0;p[j]=p[k]时,next[j+1]=next[j]+1 */
        if (k == -1 || p[j] == p[k]) {
            ++k;
            ++j;
            /*if(p[j] != p[k])*/ next[j] = k;
          //  else next[j] = next[k];
        }
        else k = next[k];
    }
}
int main()
{
    while(scanf("%s",s) && s[0] != '.') {
        int len = strlen(s);
        GetNext(s,next);
        int cc = 1;
        if(len%(len-next[len]) ==0) cc=len/(len-next[len]);
        printf("%d\n",cc);
    }
    return 0;
}
时间: 2025-01-13 15:16:09

POJ2406 Power Strings next数组应用的相关文章

POJ2406:Power Strings(后缀数组DC3)

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, exponentiation by a non-negative inte

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 = "

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]

poj 2406 Power Strings 后缀数组解法

连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们还是用后缀数组做一做,巩固后缀数组的能力. 对于一个串,如果能写出a^n这种形式,我们可以暴力枚举循环节长度L,那么后缀suffix(1)和suffix(1 + L)的LCP应该就是 lenstr - L.如果能满足,那就是,不能,就不是. 这题的话da算法还是超时,等我学了DC3再写上来. 其实这

poj2406 Power Strings

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33273   Accepted: 13825 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,后缀数组)

这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即为结果. 若lcp(suffix(0), suffix(k))的长度为n- k,则将串每k位分成一段,则第1段与第2段可匹配,又可推得第2段与第3段可匹配……一直递归下去,可知每k位都是相同的,画图可看出匹配过程类似于蛇形. 用倍增算法超时,用dc3算法2.5秒勉强过. #include<cstdi

【后缀数组】poj2406 Power Strings

连续重复子串(pku2406)给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.算法分析:做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足.判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k.在询问最长公共前缀的时候, suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间

poj2406 Power Strings 2012-01-11

http://162.105.81.212/JudgeOnline/problem?id=2406 ____________________________ 求最长重复字串.即将字符串自我匹配,然后字符串长度减去最后一位的next值即为最长重复字串的长度.注意他求的是由多少个最长重复字串组成. ____________________________ 1 Program stone; 2 var i,j:longint; 3 s:ansistring; 4 b:array[1..1000005]