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. 【JZOJ4935】【NOIP2017GDKOI模拟1.12】b

    Boring 构造一棵包含1号结点的连通子集个数刚好为给定的n的树. 这棵树的结点不能多于60. 1<=n<=109 Gai 容易得到,计算给定一棵树的Ans1,其中Ansi表示包含i号结 ...

  2. 2019.10.24TCP协程处理

    server from gevent import monkey,spawn monkey.patch_all() import socket server = socket.socket() ser ...

  3. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结 本系列文章中可能有很多翻译有问题或者错误的地方:并且有些章节 ...

  4. C++之以分隔符的形式获取字符串

    void CConvert::Split(const std::string& src, const std::string& separator, std::vector<st ...

  5. ajax请求与form表单提交共存的时候status为canceled

    chrome浏览器调试,发现,status竟然是canceled状态 网上总论: 1.在URL变更后,会对当前正在执行的ajax进求进行中止操作.中止后该请求的状态码将为canceled 2.在使用到 ...

  6. mac下的抓包工具Charles

    在mac下面,居然没有好的抓包工具,这让我十分纠结,毕竟不可能为了抓一个http包就跑到win下折腾.或许有人说tcpdump这么好的工具,你怎么不用.说实话,tcpdump太复杂了,我还没有细看,再 ...

  7. 17个你必须牢记的Win10快捷键

    电脑初学者掌握了盲打技术,可以提高录入速度:游戏玩家掌握了快捷键,可以在瞬息百变的对战中提高生存的机会:而Windows玩家掌握了快捷键,不但可以提高电脑操作速度,更能享受到初级玩家望着你那仰慕的眼神 ...

  8. Python学习之路9☞面向对象的程序设计

    一 面向对象的程序设计的由来 见概述:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二 什么是面向对象的程序设计及为什么要有它 面向过 ...

  9. html5之div,article,section区别与应用

    div 块级元素,在里面的内容会自动开始新行,可以定义文档中的分区或节,把文档分割成独立,不同的部分 本身没有什么语义,更适合帮助布局,进行样式化. <div> </div> ...

  10. 2019-3-13-win10-uwp-使用-ScaleTransform-放大某个元素

    title author date CreateTime categories win10 uwp 使用 ScaleTransform 放大某个元素 lindexi 2019-3-13 19:5:56 ...