Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5663    Accepted Submission(s): 2557

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
 
Author
possessor WC
 
Source
 

题意:给出一个字符串,问再加多少个同样序列可以构成至少里面存在两个相同的串,加的字符在原串左边右边都可以。
如:abca    加ab得到abcabc,成立
abcde加abcde得到abcdeabcde成立

#include <stdio.h>
#include <string.h>
char s[1000005];
int num[1000005];
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
int j=-1;
num[0]=-1;
scanf("%s",s);
int n=strlen(s);
for(int i=0; i<n;)
{
if(j==-1||s[i]==s[j])num[++i]=++j;
else j=num[j];
}
int cir=n-num[n];
if(n%cir==0&&cir!=n)printf("0\n");
else printf("%d\n",cir-(n-(n/cir)*cir));
}
return 0;
}

HDU 3746:Cyclic Nacklace的更多相关文章

  1. HDU 3746:Cyclic Nacklace(KMP循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

    Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...

  3. (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746

    http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...

  4. HDU 3746 Cyclic Nacklace (用kmp求循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. Cyclic Nacklace HDU - 3746 (kmp)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  8. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  9. Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)

    串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...

随机推荐

  1. Java基础之访问文件与目录——测试文件或目录的路径(TryPath)

    控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...

  2. j2ee Servlet、Filter、Listener

    首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角色,Interceptor是某些MVC框架中的角色,比如Stru ...

  3. tomcat部署方法总结

    可以参考之前的:http://www.cnblogs.com/youxin/archive/2013/01/18/2865814.html 在Tomcat中部署Java Web应用程序有两种方式:静态 ...

  4. tableview隐藏多余分割线

    - (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...

  5. PostgreSQL Insight Monitor pgstat

    PostgreSQL Insight Monitor  pgstat pgstat 是一个连接到数据库并获取数据库的活动状态的命令行工具. PostgreSQL有许多状态: archiver for ...

  6. js方法和prototype

    JS中的方法可以分为三类 1.对象方法 2.类方法 3.原型方法 例: function People(name) { this.name=name; //对象方法 this.Introduce=fu ...

  7. java正则表达式练习

    package shb.java.demo3; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达式简 ...

  8. JFreeChart在制作折线图

    JFreeChart在制作折线图的时候可以使用两种不同的方式 package Line; import java.awt.Color; import java.awt.Font; import org ...

  9. JSP-02- 使用JSP实现输出

    二. 使用JSP实现输出 JSP的页面构成: 静态内容.指令.表达式.Scriptlet.声明.动作.注释 Jsp脚本: 表达式.Scriptlet.声明 表达式: <%=  内容  %> ...

  10. java 网络编程(三)---TCP的基础级示例

    下面是TCP java网络编程的基础示例: tcp传输:客户端建立过程的思路:1.创建TCP客户端的Socket服务,使用的是socket对象,建议在创建的过程中,就明确了目的地和要连接的主机2.如果 ...