kmp求循环节——HDU3746

                                        Cyclic Nacklace

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

kmp求循环节的入门题把,让人不理解的是SB杭电数组开小了居然报TLE(⊙﹏⊙)

#include<cstdio>
#include<cstring>
using namespace std;
char b[100007];
int nxt[100007];
int len;
void getnext()
{
    nxt[0]=-1;
    len=strlen(b);
    int k=-1;
    int j=0;
    while(j<len)
    {
        if(k==-1||b[j]==b[k])
        {
            nxt[++j]=++k;;
        }
        else k=nxt[k];
    }
}
int n;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",b);
        getnext();
        len=strlen(b);
        n=len-nxt[len];//代表循环节的长度
        if(len%n==0&&n!=len) printf("0\n");//可以多次循环
        else printf("%d\n",n-nxt[len]%n);
    }
}

最后哪个模n必须要否则wa,取模的作用:abcab,去掉abc。

KMP算法的核心在于构建部分匹配表(也称为`next`数组),该表用于记录模式串的部分匹配信息。通过这些信息,可以在寻找重复子串的过程中优化查找效率。 要利用KMP算法来找出字符串的最小循环节,可以通过以下方法实现: ### 使用 KMP 找到最小循环节 对于一个长度为 `n` 的字符串 `s`,如果存在某个最小子串 `p` 能够通过多次复制构成原字符串,则这个子串即是最小循环节。以下是具体过程: #### 构建 Next 数组 定义一个辅助函数计算前缀函数值并存储于 next 数组中。此操作的时间复杂度为 O(n)[^1]。 ```python def compute_next_array(pattern): n = len(pattern) next_arr = [0] * n j = 0 for i in range(1, n): while j > 0 and pattern[i] != pattern[j]: j = next_arr[j - 1] if pattern[i] == pattern[j]: j += 1 next_arr[i] = j return next_arr ``` #### 计算最小循环节 基于上述得到的 next 数组,我们可以进一步推导出最小周期 t=n−next[n−1] 。当满足条件 (t 可整除 n),则表明 s 存在一个大小为 t 的基本单元反复拼接而成;否则整个字符串本身不具备更短的有效循环结构。 ```python def find_smallest_repeating_substring(s): next_arr = compute_next_array(s) length_of_string = len(s) period = length_of_string - next_arr[-1] if length_of_string % period == 0 and period != length_of_string: return s[:period] else: return s ``` 以上代码片段展示了如何应用 KMP 中的 next 数组特性去检测是否存在以及返回目标字符串内的最简重复序列。 ### 结论 综上所述,借助 KMP 算法不仅可以高效解决模式匹配问题,在特定场景下还能巧妙运用于诸如发现字符串内部隐藏规律的任务之中,比如本例中的定位最小循环单位即是如此。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值