Cyclic Nacklace HDU - 3746(字符串周期)

举报
用户已注销 发表于 2021/11/19 06:34:45 2021/11/19
【摘要】 题目: 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 9...

题目:

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


    
  1. 3
  2. aaa
  3. abca
  4. abcde

Sample Output


    
  1. 0
  2. 2
  3. 5

题意:给一个字符串,求至少添加多少字符才能变成周期串。

字符串周期问题用简单的next数组即可,不用改进的next数组

代码:


  
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. char c[100005];
  5. int next_[100005];
  6. int l;
  7. void get_next(char *t, int m, int next[])
  8. {
  9. int i = 1, j = 0;
  10. next[0]=next[1] = 0;
  11. while (i < m)
  12. {
  13. if (j == 0 || t[i] == t[j])next[++i] = ++j;
  14. else j = next[j];
  15. }
  16. }
  17. int main()
  18. {
  19. int t;
  20. cin >> t;
  21. gets(c + 1);
  22. while (t--)
  23. {
  24. gets(c + 1);
  25. l = strlen(c + 1);
  26. get_next(c, l, next_);
  27. int dif = l - next_[l];
  28. if (c[l] == c[next_[l]])cout << (dif - l%dif) % dif;
  29. else cout << l;
  30. cout << endl;
  31. }
  32. return 0;
  33. }

这个代码是AC的,不过却是错的

对于aabaaa这种字符串,上面的代码结果是6,实际上应该是2

于是新的代码如下:

 


  
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. char c[100005];
  5. int next_[100005];
  6. int l;
  7. void get_next(char *t, int m, int next[])
  8. {
  9. int i = 1, j = 0;
  10. next[0] = next[1] = 0;
  11. while (i < m)
  12. {
  13. if (j == 0 || t[i] == t[j])next[++i] = ++j;
  14. else j = next[j];
  15. }
  16. }
  17. int main()
  18. {
  19. int t;
  20. cin >> t;
  21. gets(c + 1);
  22. while (t--)
  23. {
  24. gets(c + 1);
  25. l = strlen(c + 1);
  26. get_next(c, l, next_);
  27. int k = next_[l];
  28. while (k && c[l] != c[k])k = next_[k];
  29. int dif = l - k;
  30. cout << (dif - l % dif) % dif + (!k)*l << endl;
  31. }
  32. return 0;
  33. }

这个代码也是AC的,看来标程并没有问题,只是数据太水

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/52131848

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。