HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace
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.
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 ).
#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数组运用)的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...
- hdu 3746 Cyclic Nacklace(kmp最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- hdu 3746 Cyclic Nacklace(next数组求最小循环节)
题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...
- hdu 3746 Cyclic Nacklace (KMP求最小循环节)
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...
- HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
随机推荐
- Bootstrap —— tab切换
tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- POP介绍与使用实践(快速上手动画)
http://adad184.com/2015/03/11/intro-to-pop/ 前言 动画在APP开发过程中 大家多多少少都会接触到 而且随着ios7的扁平化风格启用之后 越来越多的APP开始 ...
- 2018-11-21-WPF-解决-ViewBox--不显示线的问题
title author date CreateTime categories WPF 解决 ViewBox 不显示线的问题 lindexi 2018-11-21 09:37:53 +0800 201 ...
- Python中if __name__ == '__main__':理解
在很多python脚本中在最后的部分会执行一个判断语句if __name__ == "__main__:",之后还可能会有一些执行语句.那添加这个判断的目的何在? 在python编 ...
- React全家桶打造共享单车后台管理系统项目_第1篇_项目环境搭建_首页编写
1.项目介绍 项目github地址:https://github.com/replaceroot/React-manageSystem 项目整体架构: 课程大纲: 第一章:React基础知识 ...
- git 练习
删除文件 git rm test.txt git commit -m 'remove test.txt' 回复到最新版本 git checkout -- test.txt git checkout ...
- 第四次C++
继承与派生 一.什么是继承和派生 所谓继承就是从先辈处得到属性和行为特征.类的继承,是新的类从已有类那里得到已有的特性.从另一个角度来看这个问题,从已有类产生新类的过程就是类的派生.类的继承与派生机制 ...
- re模块下的常用方法
一 : re模块的查找 findall 优先级查找 返回列表 找所有的匹配项(从大段的内容中找匹配到的项目) import re str = "qwer asdf zxcv qwer ...
- Data Flow-File Read-网络距离
- 「BZOJ3694」「FJ2014集训」最短路
「BZOJ3694」「FJ2014集训」最短路 首先树剖没得说了,这里说一下并查集的做法, 对于一条非树边,它会影响的点就只有u(i),v(i)到lca,对于lca-v的路径上所有点x,都可通过1-t ...