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.

InputThe 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 ).OutputFor 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
  4.  
  5. kmp求循环节。
    推荐博客:https://www.cnblogs.com/chenxiwenruo/p/3546457.html

注意题目要求至少循环两次。所以判断条件需要写出来

  1.  
  1. #include<stdio.h>
  2. #include<string.h>
  3. int n,Next[],T,plen;
  4. char p[];
  5.  
  6. void prekmp() {
  7. plen=strlen(p);
  8. int i,j;
  9. j=Next[]=-;
  10. i=;
  11. while(i<plen) {
  12. while(j!=-&&p[i]!=p[j]) j=Next[j];
  13. if(p[++i]==p[++j]) Next[i]=Next[j];
  14. else Next[i]=j;
  15. }
  16. }
  17.  
  18. int main() {
  19. //freopen("out","r",stdin);
  20. scanf("%d",&T);
  21. while(T--) {
  22. scanf("%s",p);
  23. prekmp();
  24. //printf("%d**\n",Next[plen]);
  25. int l=plen-Next[plen];
  26. if(plen%l==&&plen/l>) printf("0\n");
  27. else printf("%d\n",l-plen%l);
  28. }
  29. }
 

kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

随机推荐

  1. Flash 零日漏洞复现(CVE-2018-4878)

    项目地址:https://github.com/Sch01ar/CVE-2018-4878.git 影响版本为:Adobe Flash Player <= 28.0.0.137 攻击机器IP:1 ...

  2. 第十四章 Spring MVC的工作机制与设计模式(待续)

    Spring MVC的总体设计 Control设计 Model设计 View设计 框架设计的思考 设计模式解析之模版模式

  3. Java中弱引用、软引用、虚引用及强引用的区别

    Java中弱引用VS软引用 Java中有如下四种类型的引用: 强引用(Strong Reference) 弱引用(WeakReference) 软引用(SoftReference) 虚引用(Phant ...

  4. 【263】Linux 添加环境变量 & 全局 shell 脚本

    Linux电脑添加环境变量 方法一:通过修改 profile 文件添加环境变量 1. 打开终端,输入[vi /etc/profile],如下所示,点击回车 [ocean@ygs-jhyang-w1 L ...

  5. find查找、split分隔、replace替换

    #!/usr/bin/env python r = "asada" ret = r.find("d") print(ret)#返回所在位置的索引 ret =r. ...

  6. 全局事务/分布式事务 (Global Transaction/ A distributed transaction)之我见

    这里参考的是Oracle对于XA的支持,其他的应该雷同吧... 1个分布式事务由多个行为在不同的数据库上执行,1个分布式事务的执行成功意味着相关数据库上的行为执行均成功.“XA协定”(http://w ...

  7. CDN原理解析

    首先,让我们来看一下传统的Internet网络的基本结构和数据传输情况,如下图所示 根据传统的网络结构,用户的访问流程基本如下:  用户在自己的浏览器中输入要访问的网站的域名  浏览器向本地DNS请求 ...

  8. go 语言介绍

    特点1. 静态类型,编译开源语言 2. 脚本化的语法,支持多种编程范式(函数式,面向对象) 3. 原生,给力的并发支持并发编程 Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+ ...

  9. 771. Jewels and Stones珠宝数组和石头数组中的字母对应

    [抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...

  10. 624. Maximum Distance in Arrays二重数组中的最大差值距离

    [抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...