Cyclic Nacklace

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
题目大意:给定一个字符串,问你最少需要添加多少个‘a'~'z'的字符使它成为一个循环的字符串
思路:kmp,由kmp我们可以知道next数组的作用是求得子串s每个位置的相同前缀后缀的个数,不过我这里的nex数组是从{-1,0,1,2} 分别代表相同个数有{0,1,2,3}这么多个,那么我们可以看到next[len]也就是整个子串的相同前后缀个数,如果该个数等于0,则说明要想构成一个循环还需要一个子串的长度,所以此时需要len个,其次如果该个数==len-1 就说明子串已经构成了循环不需要在添加字符,最后只有部分相同的话len-nex[len]就是他的最小循环节,需要加上len-nex[len] - len%(len-nex[len])..不过后面看到一篇博客给出了kmp字符串循环节的解释,感觉emm超简洁 哈哈哈哈 大家可以去学学 http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html
 
 #include<iostream>
#include<algorithm>
#include<string> using namespace std;
const int maxn = ;
int nex[maxn], T;
void Getnext(string a, int len)
{
nex[] = -; int k = -;
for (int i = ; i < len; i++) {
while (k > - && a[k + ] != a[i])k = nex[k];
if (a[k + ] == a[i])k++;
nex[i] = k;
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> T;
string str;
while (T--) {
cin >> str; int len = str.length();
Getnext(str, len);
int k = len - (nex[len - ] + );
if (len%k == && len != k)cout << "" << endl;
else cout << k - len % k << endl;
}
return ;
}

HDU 3746 Cyclic Nacklace(kmp next数组运用)的更多相关文章

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

    题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...

  2. hdu 3746 Cyclic Nacklace KMP循环节

    Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...

  3. HDU 3746 Cyclic Nacklace KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...

  4. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  5. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  6. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  7. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

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

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

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

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

随机推荐

  1. Mac上定时运行脚本工具--launchctl

    在Mac上可以像在Linux上一样,使用crontab来定时运行脚本,但苹果并不推荐这个方法.苹果推荐使用Launchctl来完成定时任务. 首先,我们先写一个可执行的脚本,列子为php脚本,名字为t ...

  2. Intersection of Two Linked Lists两链表找重合节点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. Oracle 定义变量总结

    首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...

  4. 带三角形下标的提示框(按钮button)

    HTML:<div class="leaflet-popup-content-wrapper"> <div class="leaflet-popup-c ...

  5. Linux上编辑然后执行一段脚本的机制

    简要分析下刚开始提出的第二个问题, 因为没看代码,所以只是简单流程 1. 在bash里打开vim编辑文件并保存退出: bash进程fork子进程, 然后调用exec装入vim程序,wait这个子进程v ...

  6. 开启远程XUL

    参考:https://developer.mozilla.org/zh-cn/Remote_XUL firefox自4.0起(2011年4月版本,目前最新版为13.0),开始禁用远程XUL,这阻碍了初 ...

  7. 三.使用JDBC处理MySql大数据

    一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像.声音.二进制文等. 在实际开发中,有时 ...

  8. Person Re-identification 系列论文笔记(一):Scalable Person Re-identification: A Benchmark

    打算整理一个关于Person Re-identification的系列论文笔记,主要记录近年CNN快速发展中的部分有亮点和借鉴意义的论文. 论文笔记流程采用contributions->algo ...

  9. 在centos搭建git服务器时,不小心把/home/git目录删除了,我是怎么恢复的

    在centos搭建git服务器时,不小心把/home/git目录删除了,我是怎么恢复的 在删除掉/home/git目录后,每次 git push提交时,都让填写密码,烦 第一步:在本地找到id_rsa ...

  10. 如何实现两个input框的同步更新

    class MVVM { constructor(options) { this.$options = options; this.$el = options.el; this.$data = opt ...