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 <cstdio>
#include <cstring>
using namespace std; const int MS=;
char str1[MS],str2[MS];
int next[MS];
void get_next(char *s,int *next)
{
int i=,j=;
next[]=;
int len=strlen(s);
while(i<len)
{
if(j==||s[i-]==s[j-])
{
i++;
j++;
next[i]=j; //求最大循环次数 就用这个
/*
if(s[i-1]==s[j-1])
next[i]=next[j];
else
next[i]=j;
*/
}
else
j=next[j];
}
} int KMP(char *s,char *t)
{
int i=,j=;
int len1=strlen(s);
int len2=strlen(t);
get_next(t,next);
while(i<=len1&&j<=len2)
{
if(j==||s[i-]==s[j-])
{
i++;
j++;
}
else
j=next[j];
}
if(j>len2)
return i-len2-;
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str2);
get_next(str2,next);
int len=strlen(str2);
if(str2[len-]!=str2[next[len]-])
next[len]=next[next[len]];
int ans=len-next[len];
if(ans!=len&&len%ans==)
printf("0\n");
else
printf("%d\n",ans-len%ans);
}
return ;
}
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 ...
随机推荐
- axis2学习, ant 构建axis2 ws
1,axis2安装(windows) . 环境需求:jdk 1.5, tomcat 6, maven 2, ant . 下载(bin表示二进制文件):http://mirror.bjtu.edu. ...
- .NET文件上传的大小限制配置
<system.web> <!--maxRequestLength单位是Kb--> <httpRuntime maxRequestLength="20971 ...
- Linux /proc 的意义
/proc 是个虚拟文件系统.也就是.重新引导后修改会被重新初始化 提供了进程信息.内存资源.硬件设备.内核内存等信息 比如: 网卡:/proc/sys/vm/ipv4/ip_ ...
- 启动tomcat时 一闪而过解决方法
1 首先确定JAVA 已经配好了环境变量,具体配置方法,找一下度娘. 测试方法:进入cmd -> javac -version 能看到JAVA的版本信息,证明配置成功了. 2 分析一下问题出现的 ...
- 可以binidng属性的属性【项目】
1:binding后台bool[]数据以及后台ObservableCollection数据 分别见下面xaml的Visibility和Text的Binding public bool[] Rubber ...
- HTML to DOM
Although you can now natively parse HTML using DOMParser and XMLHttpRequest, this is a new feature t ...
- (剑指Offer)面试题19:二叉树的镜像
题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...
- css ie7中overflow:hidden失效问题及解决方法
css兼容ie7: 做页面的时候用负边距居中的时候在IE7下面,父节点中的overflow:hiden失效的问题,查阅了一些资料,总结一下解决方法. 问题原因: 当父元素的直接子元素或者下级子元素的样 ...
- 安装配置tomcat环境
安装配置tomcat环境 #所需要软件包 apache-tomcat-7.0.65.tar.gz jdk-7u80-linux-x64.gz #建立 个专用账户 usradd tomcat ...
- Adobe Photoshop CC 14.0简体中文特别版32位和64位下载
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...