HDU3746 Cyclic Nacklace (kmp补齐循环节使成为循环串至少应添加几个字符)

此博客介绍了如何利用KMP算法解决一个关于制作具有循环节的链式手链的问题,旨在以最少的成本添加珍珠。题目要求在串的头部或尾部添加字符,使得串至少有两个循环节。博主提供了分析思路和代码实现,通过检查原始串的循环节特性来确定所需添加的最少珍珠数量。
摘要由CSDN通过智能技术生成

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. 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.

 

Input 

The 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 ).

 

Output

For 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

 

题意:

给你一个串,要你在串头或尾添加最少的字符,使得该串至少有2个循环节,问你最少需要加几个字符.

 

 

思路:

  • 如果一个串需要至少添加x(x>=0)个字符才能是有>=2个循环节的串,那么可以只在串末尾添加,不需要去串头添加.(例如串cabc,循环节是abc,我可以在尾部添加ab即可.和在头部添加ab一样)
  • 如果原始串已经有至少两个循环节就不必添加.当f[m]>0&&m%(m-f[m])==0时,不必添加.
  • 如果n%(n-f[n])==0,则存在重复连续子串,最小循环节长度为n-f[n].  K= n/(n-f[n]) 即为所求的循环节个数.

  • S不论是不完整的循环串还是完整的循环串,n-f[n]一定是串S中去除末尾残缺部分之后,存在的最小循环节长度

  • 符合 n%(n-f[n])==0 && f[n] != 0 , 则说明字符串循环,而且循环节长度为:  n- f[n] , 循环次数为: n/(n-f[n])

 

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXM=100000+100;
int f[MAXM],m;
char P[MAXM];
void getFail(char *P,int *f)
{
    f[0]=f[1]=0;
    for(int i=1;i<m;i++)
    {
        int j=f[i];
        while(j && P[i]!=P[j]) j=f[j];
        f[i+1]= (P[i]==P[j])?j+1:0;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",P);
        m=strlen(P);
        getFail(P,f);
        if(f[m]==0)//说明没有前缀等于后缀,所以直接扩大一倍串
        {
            printf("%d\n",m);
            continue;
        }
        if(m%(m-f[m])==0) printf("0\n");//执行此if以及else证明上面的if条件不满足,即存在前缀等于后缀,再看需要添加几个字符.如果if条件满足,则证明已满足循环串条件.
        else//看需要添加几个字符构成循环串
        {
            int len1=m-f[m];//循环节的长度
            printf("%d\n",len1-m%len1);//需要添加的字符的长度为循环节长度减去循环节的残缺部分长度
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值