HDU3746 Cyclic Nacklace 【KMP】
Cyclic Nacklace
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.
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
).
3
aaa
abca
abcde
0
2
5
题意:给定一个串。求至少加入几个字符能使其内部至少反复两次的反复串。
题解:直接对所给串求next数组,len-next[len]即为循环节点。
#include <stdio.h>
#define maxn 100002 char str[maxn];
int len, next[maxn]; void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
++i; ++j; next[i] = j;
}else j = next[j];
}
len = i; //save time
} int main()
{
int cas, ans;
scanf("%d", &cas);
while(cas--){
scanf("%s", str);
getNext();
ans = len - next[len];
if(ans != len && len % ans == 0)
ans = 0;
else ans = ans - next[len] % ans;
printf("%d\n", ans);
}
return 0;
}
HDU3746 Cyclic Nacklace 【KMP】的更多相关文章
- hdu3746 Cyclic Nacklace【nxt数组应用】【最小循环节】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdoj 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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU_3746 Cyclic Nacklace 【KMP的应用】
一.题目 HDU3746 二.分析 KMP比较好解决的一个问题:如果求一个串中的循环节? 仔细回想KMP的用法,重点是next数组,相当于就是后缀和前缀的比较,那么不正是方便了我们确定循环节? 如果以 ...
- HDU3746 Cyclic Nacklace —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) M ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
随机推荐
- simplemodal — jquery弹出窗体插件
方式一:使用jquery-1.7.1.min.js(1.9.1的版本我试过了,不行) + jquery_modal.js的方式 文件: testModel.css: /* Overlay ...
- SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurer、TilesViewResolver、<put-attribute>、<t:insertAttribute>)
一. 1.定义TilesConfigurer.TilesViewResolver的bean 注意有tiles2和tiles3,这里使用tiles3 (1)java形式 package spittr.w ...
- uml类图的几种关系
UML类图几种关系的总结 在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregati ...
- 对XX证券报关于物联网操作系统的几个问题的答复
XX证券报提问了几个关于物联网和物联网操作系统的问题,个人表达了一些粗陋的观点,在这里发表出来,与行业朋友交流和探讨. 物联网行业最需要解决的问题是什么? 虽然物联网这个行业被炒得比较热,但是截至目前 ...
- Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
上传 服务器端PHP 代码如下 : <?php $target_path = "./tmp/";//接收文件目录 $target_path = $target_path.($ ...
- 图文教您轻松学会用PS设计制作名片
图文教您轻松学会用PS设计制作名片 http://jingyan.baidu.com/article/49711c614e7370fa441b7ca3.html
- python获取当前路径几种方式
__python中操作路径,最好用绝对路径.__这样会避免在多个脚本调用过程中或不同层级调用过程中出现混乱 os.getcwd() sys.argv[0] or sys.path[0] __file_ ...
- 性能测试vs负载测试vs压力测试-概念普及
下面我们主要介绍性能测试.负载测试和压力测试. 效率作为ISO 9126内部和外部质量的重要质量属性之一,其含义是在规定条件下,相对于所用的资源的数量,软件产品可提供适当性能的能力.资源可能包括其他软 ...
- visualbox使用(二)
1.安装VirtualBox的[增强功能]2.VirtualBox的[设备]->[共享文件夹],添加固定分配,如D:\Java, 名称Java3.执行如下命令#cd /mnt#mkdir w_j ...
- C#语法知识笔记
抽象类 1.抽象类没有方法体,直接在括号后加“;”. protected abstract string getShoutSound(); 2.抽象类不能实例化:抽象方法必须被子类重写:如果类中包含抽 ...