HDU 3746|Cyclic Nacklace|KMP求最小循环节

题目大意

给定字符串,问最少在末尾加多少个字符就可以让字符串存在循环节(循环节出现次数>1)。

题解

分析KMP的next数组,next[n]指最长的那个字符串是该字符串的前缀和后缀(但不是整个字符串)。

情况1:

那么如果类似ABCDABKKABCDAB这种情况,next[n]就指ABCDAB的长度=6。那么我们再补2个KK就可以了,最小循环节ABCDABKK=8,是(原长14)-6=8。
ABCDABKKABCDABKKABCDAB这种情况,|S|=22,next[n]=14,这种情况下也补KK,考虑到有出现次数超过2的循环节,可以合并这些循环节,那么合并后的最长字符串R可以是原串的前缀,也可以是其后缀(但不是整个串),而且这个合并后的最长字符串的长度|R|=next[n],第一次出现在suf(1),第二次出现在suf(n-next[n]+1),由于我们合并到最长字符串了,那么显然最小循环节的长度就是n-next[n]了(suf(1)-suf(n-next[n]),同一个串在1处又在n-next[n]+1处出现,那么S[1..n-next[n]]这部分,是这个串的前缀,既能出现在这个串的[1..n-next[n]]处(前缀串,下图红色部分),又能出现在[n-next[n]+1,2n-2next[n]]处(后缀串的前缀,下图橙色部分,此时又在前缀串出现第二次,下图蓝色部分,所以也在后缀串出现第二次(再说一遍前后缀串一样),下图紫色部分,对齐前后缀串可以发现必然在前缀串出现第三次(超出原串部分就是我们要补得)以此类推,显然是最小循环节(最小是应为我们最大化了前后缀串))。那么显然应该补n-next[n]-n%(n-next[n])。(n%(n-next[n])表示最末尾的那个不完整的循环节长度)


ABCDABKKABCDABKKABCDAB              的R=ABCDABKKABCDABKK
ABCDABKKABCDABKK
        ABCDABKKABCDABKK

上面极力想证明的就是n-next[n]-n%(n-next[n])的含义。
那么以后就可以记结论啦,n-next[n]就是最小循环串长度啦。

情况2

ABCDE这种前后缀无相同部分的情况,直接补5个字符,就是next[n]=0的情况。

情况3

还有一种情况就是不需要补的情况,这时n%(n-next[n])=0,特别判断一下就可以了。

#include <cstdio>
#include <cstring>
const int N = 100005;
char p[N];
int next[N];
int main() {
    int t, i, j, n, loop;
    scanf("%d", &t);
    while (t--) {
        scanf("%s", p + 1);
        n = strlen(p + 1);
        for (i = 0, j = 2; j <= n; ++j) {
            while (i && p[j] != p[i + 1]) i = next[i];
            if (p[j] == p[i + 1]) ++i;
            next[j] = i;
        }
        loop = n - next[n];
        printf("%d\n", !next[n] ? n : (n % loop == 0 ? 0 : loop - n % loop));
    }
    return 0;
}

Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7417 Accepted Submission(s): 3230

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值