D - 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.
InputThe 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 ).OutputFor 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 求出串中最长的相同前缀后缀长度,最短循环节 = 字符串长度 -Next[l]
向串中添加字符让它成为循环串,如果最短循环节长度能被字符串长度整除
那么无需调整
否则 需要添加最短循环节长度减去 Next[l]%最短循环节长度
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
KMP 查找子串出现的次数
*/
char t[MAXN];
int Next[MAXN];
void pre_kmp(int m)
{
int j,k;
j = ;k = -;Next[] = -;
while(j<m)
if(k==-||t[k]==t[j])
Next[++j] = ++k;
else
k = Next[k];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",t);
int l = strlen(t);
pre_kmp(l);
int r = l - Next[l];
cout<<r<<' '<<l<<' '<<Next[l]<<endl;
if(r!=l&&l%r==)
printf("0\n");
else
printf("%d\n",r-Next[l]%r);
}
}
D - Cyclic Nacklace的更多相关文章
- Cyclic Nacklace[HDU3746]
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3746:Cyclic Nacklace
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu-----(3746)Cyclic Nacklace(kmp)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- HDU3746 Cyclic Nacklace 【KMP】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 3746 Cyclic Nacklace【KMP求在结尾加上多少个字符可以使字符串至少有两次循环】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu3746 Cyclic Nacklace【nxt数组应用】【最小循环节】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3746:Cyclic Nacklace(KMP循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...
- HDU 3746 Cyclic Nacklace (用kmp求循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- E20170626-gg
occupy vt. 占领; 使用,住在…; 使从事,使忙碌; 任职; stack n. 垛,干草堆; (一排) 烟囱; 层积; 整个的藏书架排列;
- linux修改yum源
在自己安装的CentOS6.5中使用yum安装软件,总是提示404错误信息,百度后发现原来要设置yum源. 在安装完CentOS后一般需要修改yum源,才能够在安装更新rpm包时获得比较理想的速度.国 ...
- P3199 [HNOI2009]最小圈
传送门 据rqy说有这么一个结论\[ans=\min_{v \in V,F_n(v)\neq \infty} \max_{0 \leq k \leq n - 1} \left[\frac{F_n(v) ...
- 拼接sql ()
SELECT ID,FORMNAME,NODENAME,SEQUENCE, NAME, STATE, NOWTIMES, WORK.FQREALNAME||'('||FQDEPT.FULLN ...
- Coursera公开课-Machine_learing:编程作业8(2016-10-06 20:49)
Anomaly Detection and Recommender Systems 本周编程作业分为两部分:异常检测和推荐系统. 异常检测:本质就是使用样本的到特种值的gaussian分布,来预估正确 ...
- Web页面使用VLC播放插件
一.原生态Demo下载 选择原因:我们为什么选择VLC播放插件?原因是它支持IE8浏览器播放视频,如果高版本的浏览器大可不必选择该插件,很多html5插件既好用又简单,但是有些交管或政府 部门还是限制 ...
- 探索java世界中的日志奥秘
java日志简单介绍 对于一个应用程序来说日志记录是必不可少的一部分.线上问题追踪,基于日志的业务逻辑统计分析等都离不日志.JAVA领域存在多种日志框架,目前常用的日志 ...
- 移动web——媒体查询
基本概念 响应式开发在没有媒体查询前,也可以通过js来实现,但是人们基本不会考虑,特别繁琐.在出现了媒体查询,才开始逐渐推广响应式.实际开发中,在时间与金钱充足的情况下还是别做响应式,影响性能,维护麻 ...
- SQL基本操作——case end
case end进行多条件的判断 --查看Person表 select * from Person --对math字段进行条件判断 select name,数学成绩= case then '优' th ...
- JQuery文档加载完成执行js的几种方法
js中文档加载完毕.一般在body加一个onload事件或者window.onload = function () {} jQuery中有好多写法,平时也不注意,别人一问,还真觉得头大. 下面是我整理 ...