HDU-3746-Cyclic Nacklace(KMP,循环节)
Cyclic Nacklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15422 Accepted Submission(s): 6425
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
分析
- 题意:将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数。
- 必须找出最小循环节,才可以求出满足题意中的最小添加数。
- KMP算法中,可以利用k = len - nxt[len]来求出最小循环节长度k。
- KMP算法中匹配串b指针j的移动可以理解为长度为最小循环节的移动。
- 然后只需要判断在最小循环节数大于1的情况下,串是否是由整数个最小循环子串构成。如果是,就不需要添加。
- 如果需要添加,则只需要串长对最小循环节数取余求出多余长度,然后最小循环长度减去这个多余长度,即可求出需要添加数目。
string a;
int n;
int nxt[100001];
void getnxt()
{
nxt[0] = -1;
int j = 0,k = -1;
while(j<n)
{
if(k==-1||a[j] == a[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>a;
n = a.length();
getnxt();
int k = n-nxt[n];//k为最小循环节长度
if(n%k==0&&k!=n)cout<<0<<endl;//不需要添加
else
cout<<k-nxt[n]%k<<endl;
}
return 0;
}
HDU-3746-Cyclic Nacklace(KMP,循环节)的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...
- 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最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
- hdu 3746 Cyclic Nacklace (KMP求最小循环节)
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...
- 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: 给你一个字符串,让你在后面加尽 ...
- HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解
思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...
随机推荐
- Swift4 检验变量
创建: 2018/05/03 判断类 public func isKind(of aClass: Swift.AnyClass) -> Bool 是否是aClass或其子类的实例 publ ...
- E20180408-hm
interaction n. 互动; 一起活动; 合作; 互相影响; interface n. 界面; <计>接口; 交界面; v. (使通过界面或接口) 接合,连接; [计算机] ...
- POJ2369【循环节】
题意: 给一个序列,他需要几步就能变成原来的序列. 思路: 那么就是找一下各个循环节(用dfs随便搞了-),求一下最小公倍数就好了. 贴一发挫code- //#include <bits/std ...
- poj2239 poj1274【二分匹配】
题意: 就是尽可能的选多的课 思路: 把课程和上课的时间看作二分图 跑一跑二分匹配就好了 #include<iostream> #include<cstdio> #includ ...
- 浅谈欧拉函数 By cellur925
1.某神犇Blog 学了三遍的 欧拉函数φ--DEADFISH7 2.我要做一些补充o(* ̄▽ ̄*)o $φ(1)=1$: 公式有两种形式,一种有太多除法,实际可能会慢些.通用 对于任意$n$> ...
- decltype使用
#include<thread> #include<array> #include<iostream> #include<windows.h> #inc ...
- LightOj 1076 - Get the Containers (折半枚举好题)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少 ...
- 洛谷 P1072 Hankson 的趣味题 || 打质数表的分解质因数
方法就是枚举,根据b0和b1可以大大减小枚举范围,方法类似这个http://blog.csdn.net/hehe_54321/article/details/76021615 将b0和b1都分解质因数 ...
- C. Tennis Championship dp递推 || 找规律
http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...
- HashMap的简单实现
基本概念 Map 别名映射表,也叫关联数组,基本思想是它维护的键-值(对)关联,因此可以用键查找值,也有放入键值的操作,下面根据定义自己来实现一个Map,首先应该想到的是数组,因为大多数Java集合类 ...