标签:HDU Nacklace 补全 3746 next 后缀 循环 include he
Cyclic Nacklace HDU - 3746
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次以上,求补几个字符
比如
aaa+0->aaa
abca+2->abcabc
abcde+5->abcdeabcde
解:
首先这里用到一个kmp算法的最长相等前后缀算法next[]
然后很重要的一点:补全后字符串循环体L为strlen(s)-next[n]
这里有三种可能性:
1,字符串里已经有两次以上循环,而且都已补全
这种就不用补全了,判断方式:
相等前后缀肯定不为零,
且总长n%L==0
2,字符串里已经有两次以上循环,后续循环未补全
这种要把单次循环的补全,
由于已经有两次以上的循环,那么前后缀肯定要比单次循环要长(懒得证明)
那么 前后缀%单次循环 就是循环剩下来的:
比如 ababa 前后缀是aba 单次循环是 ab 剩下来的就是a
那么我们要补全的就是 单词循环-剩下来的: b
ababa + b = ababab
3, 字符串里没有两次以上循环
这种要补全两次
由于没有两次以上的循环,那么前后缀肯定要比单次循环要短(同上)
那么我们就输出 单词循环-前后缀: l-next[n]
这种可以和第二种合并
代码
#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 1000005
using namespace std;
char s[MAXN],c[MAXN];
long long next[MAXN];
int main()
{
int n,m,i,j,T;
ll ans,num;
cin>>T;
while (T--)
{
ans=0;
scanf("%s",&s);
n=strlen(s);
i=0,j=-1;
next[0]=-1;
while(i<n){
if(j==-1||s[i]==s[j]){
i++;j++;
next[i]=j;
}else{
j=next[j];
}
}
int l=n-next[n];
if(next[n]!=0&&n%l==0)
cout<<0<<endl;
else
cout<<l-next[n]%l<<endl;
}
return 0;
}
标签:HDU,Nacklace,补全,3746,next,后缀,循环,include,he 来源: https://www.cnblogs.com/vcqvcq/p/10348962.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。