HDU - 3746 KMP (循环节

版权声明:欢迎加好友讨论~ QQ1336347798 https://blog.csdn.net/henu_xujiu/article/details/81774588

题目链接

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,但是只用到了nxet数组而已,也就是只需要自我匹配,不需要与母串匹配。

next 可以数很难理解了,但要是参照博客想明白也是非常好理解的

扫描二维码关注公众号,回复: 5015385 查看本文章

例如

串a[7]=abababab
next[i]也就是除去a[0]与a[i],到a[i]之前有几位可循环。
此串next数组为
-1 0 0 1 2 3 4 5 6
注意要算到next[8]

上图是本人所理解的next数组仅供参考

好了知道next数组后怎么求循环节的个数呢,很简单

int m=len-Next[len];
cout<<m <<endl; //每个循环节的长度
cout<<len/m<<endl; //循环几次 

那么这个问题就算是解决了

ac代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int Next[1100000] ;
char str[1100000] ;
void getNext(int l)//Next
{
    int j = 0 , k = -1 ;
    Next[0] = -1 ;
    while(j < l)
    {
        if( k == -1 || str[j] == str[k] )
        {
            j++ ;
            k++ ;
            Next[j] = k ;
        }
        else
            k = Next[k] ;
    }
}
int main()
{
    int n;
    scanf("%d",&n); 
    while(n--)  
	  {
    	scanf("%s",str);
        int len = strlen(str);
        getNext(len);
        int m=len-Next[len];
		//cout<<m<<endl; //循环节 
		
        if(len%m==0&&len/m>=2) cout<<0<<endl;//循环完 
		else cout<<m-len%m<<endl;  
    }
    return 0;
}

关于next数组的优化

来看第一个例子:

显然,当我们上边的算法得到的next数组应该是[ -1,0,0,1 ]

所以下一步我们应该是把j移动到第1个元素咯:

不难发现,这一步是完全没有意义的。因为后面的B已经不匹配了,那前面的B也一定是不匹配的,同样的情况其实还发生在第2个元素A上。

显然,发生问题的原因在于P[j] == P[next[j]]

所以我们也只需要添加一个判断条件即可:

public static int[] getNext(String ps) {

    char[] p = ps.toCharArray();

    int[] next = new int[p.length];

    next[0] = -1;

    int j = 0;

    int k = -1;

    while (j < p.length - 1) {

       if (k == -1 || p[j] == p[k]) {

           if (p[++j] == p[++k]) { // 当两个字符相等时要跳过

              next[j] = next[k];

           } else {

              next[j] = k;

           }

       } else {

           k = next[k];

       }

    }

    return next;

}

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81774588