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 ...
随机推荐
- 邮件发送服务AWS SES,Mailgun以及SendCloud(转)
原文:http://www.l4zy.com/posts/aws_ses-mailgun-sendcloud.html 电子邮件这一已经诞生很多年的互联网基础服务并没有随着时间的推移而慢慢消亡,实际上 ...
- [翻译]创建ASP.NET WebApi RESTful 服务(10)
通过URI实现版本管理 另一种实现版本管理的方式就是通过URI来进行处理,类似于http://localhost:{your_port}/api/v1/students/.这种方式的好处是使用者可以清 ...
- ArcObjects10.0MapControl不显示地图内容
添加MapControl控件,右键属性设置MXD 文档之后,运行显示一片空白.拖放另一个控件axLicenseControl之后OK.
- Hibernate资源
正在学马士兵Hibernate的同学来看这里,这里提供了他视频里需要的JAR包,请尽情下载,给好评喔. 一.Hibernate 3.3.2 核心JAR包 http://pan.baidu.com/s/ ...
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- AJAX小练习,防止以后忘记
<div id="content"> <input id="btnShow" type="button" value=&q ...
- mssql 用户只能查看授权的数据库
问题背景:公司的一台数据库服务器上放在多个数据库,每个数据库都使用不同的登录名称,但在将项目文件发布到Ftp时,有些Ftp的信息是在客户那边的 一旦客户那边使用配置文件中的数据库信息连接到数据库他就能 ...
- Spring REST实践之Spring Boot
Spring Boot基本描述 可以利用http://start.spring.io网站的进行Spring Boot的初始化构建.这个初始化构建器允许你输入工程基本信息.挑选工程支持的功能,最后会生成 ...
- 如何在tomcat安装部署php项目
java开发者都知道,tomcat是用来部署java web项目的.前几天老k偶然得知PHP/Java Bridge,通过它可以实现在jsp和php之间共享session,详见<如何实现jsp和 ...
- application与cache
每个项目都有一些全局,常用的信息,而这些信息如果在每次使用时都载入,那必将耗费很大的资源,特别是对访问压力大的系统.因此,这个情况中,把这些全局信息放到缓存中是很必要的,放在缓存中可以使得数据能够很快 ...