HDU3746 Cyclic Nacklace KMP求循环节

HDU3746给定一个字符串,求:在该字符串末尾最少补充多少个字符,可以使得这个字符串获得周期性。
周期性指存在一个子串,使得该字符串可以正好分解成若干个这个子串(数量要大于1)。

Input

第一行是一个整数 T ( 0<T<=100 ) 代表测试数据的组数。
之后T行每行一个字符串,由小写字母组成,字符串的长度3<=L<=100000。

Output

每组数据输出一行结果。

Sample Input

3
AAA
ABCA
ABCDE

Sample Output

0
2
5

本题有一个比较重要的性质:m-next[m] == 最小循环节长度
#include <bits/stdc++.h>
using namespace std;
int T;
int m,n;
int nxt[100005];
char a[100005];

void getnxt(){
    int j,i;
    j = -1;
    i = 0;
    nxt[0] = -1;
    while(i < m){
        while(j != -1 && a[j] != a[i]) j = nxt[j];
        nxt[++i] = ++j;
    }
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%s",a);
        m = strlen(a);
        getnxt();
        n = m - nxt[m];
        if (n != m && m % n == 0) puts("0");
            else printf("%d\n",n - m%n);
    }
    return 0;
}

同类题目:HDU1358

#include <bits/stdc++.h>
using namespace std;
int n;
char a[1000005];
int nxt[1000005];

void getnxt(){
    int i = 0,j = -1;
    nxt[0] = -1;
    while(i < n){
        while(j != -1 && a[j] != a[i]) j = nxt[j];
        nxt[++i] = ++j;
    }
}

int main(){
    for (int t = 1;scanf("%d",&n) && n;t++){
        scanf("%s",a);
        getnxt();
        printf("Test case #%d\n",t);
        for (int i = 1;i <= n;++i){
            int m = i - nxt[i];
            if (i % m == 0 && i != m){
                printf("%d %d\n",i,i/m);
            }
        }
        puts("");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/mizersy/p/9530481.html

时间: 2024-10-11 16:17:03

HDU3746 Cyclic Nacklace KMP求循环节的相关文章

HDU 3746 Cyclic Nacklace (KMP找循环节)

Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Bein

HDU 3746 Cyclic Nacklace (KMP找循环节)

题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd "Vegetable-Birds Cup" Programming Open Contest Solution 题意 给定一个字符串,问至少需要在末尾添加多少个字符使得字符串循环. 思路 KMP 设前缀函数为 \(\pi()\),字符串长度为 \(n\),下标从 \(1\) 开始. 最小循环

hdu 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i

HDU 3746 Cyclic Nacklace(KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

HDU - 3746 Cyclic Nacklace (KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

hdu3746 kmp求循环节

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the en

17999 Light-bot 模拟 + kmp求循环节

http://acm.scau.edu.cn:8000/uoj/mainMenu.html 17999 Light-bot 时间限制:1000MS  内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description I (you needn't know who am "I".) am currently playing a game called "Light-bot". In the game, the "

【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)

求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度. #include <cstdio> #define N 100005 int n,next[N]; char s[N]; int main(){ scanf("%d",&n); while(n--){ scanf("%s",s); int i=0,k=-1; next[0]=k; while(s[i]){ if(k==-1||s[i]==s[k]) { i++; k++; ne

hdu 3746 kmp求循环节

题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=10