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 entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl‘s fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls‘ lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet‘s cycle is 9 and its cyclic count is 2: 

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden. 
CC is satisfied with his ideas and ask you for help.

InputThe first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases. 
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by ‘a‘ ~‘z‘ characters. The length of the string Len: ( 3 <= Len <= 100000 ).OutputFor each case, you are required to output the minimum count of pearls added to make a CharmBracelet.Sample Input

3
aaa
abca
abcde

Sample Output

0
2
5题意:给一个字符串你,求需要在末尾加几个字符使它变成最少两个循环的题解:利用Next数组的性质,循环节就是slen-Next【slen-1】-1,刚开始自己推出来了,结果细节没考虑到):

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

using namespace std;

const double g=10.0,eps=1e-9;
const int N=100000+5,maxn=1000000+5,inf=0x3f3f3f3f;

int Next[N],slen,plen;
string str,ptr;

void getnext()
{
    int k=-1;
    Next[0]=-1;
    for(int i=1;i<slen;i++)
    {
        while(k>-1&&str[k+1]!=str[i])k=Next[k];
        if(str[k+1]==str[i])k++;
        Next[i]=k;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    int t;
    cin>>t;
    while(t--){
        cin>>str;
        slen=str.size();
        getnext();
        if(Next[slen-1]==-1)
        {
            cout<<slen<<endl;
            continue;
        }
        int ans=slen-Next[slen-1]-1;
        if(slen%ans==0)cout<<0<<endl;
        else cout<<ans-slen%ans<<endl;
    }
    return 0;
}

时间: 2024-10-16 19:31:35

hdu3746 kmp求循环节的相关文章

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 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 本题

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

区间DP+next求循环节 uva 6876

1 // 区间DP+next求循环节 uva 6876 2 // 题意:化简字符串 并表示出来 3 // 思路:dp[i][j]表示 i到j的最小长度 4 // 分成两部分 再求一个循环节 5 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cstdio> 10 #include <vector> 11 #include <cmath

HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> 312 -> 231 -> 123 因为:312>123, 231>123, 123=123 所以答案是:0 1 2 2.令str1=s,str2=s+s,然后str1作为子串,str2作为主串,进行扩展kmp求出str2[i...len2-1]与str1[0...len1-1]的最长

hdu4333 Revolving Digits(扩展kmp+kmp最小循环节)

题目链接:点击打开链接 题意描述:给定一个字符串,可以把字符串的后x位移到字符串前面组成一个新的字符串?问对于所有的新组成的字符串中去掉重复的之后,比原串小的个数,等于的个数,大于的个数? 解题思路:扩展KMP(next1[i]数组含义:x[i···len-1]与x[0···len-1]的最长公共前缀) 分析:首先我们把字符串s复制一遍接到原字符串后面形成ss,这样在ss中以i(i>=0&&i<len)为起点的长度为len的字符串就是所有可能的新字符串: 讲到这里,问题转变为在