博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher D - Cyclic Nacklace HDU - 3746(循环节kmp)...
阅读量:4556 次
发布时间:2019-06-08

本文共 2962 字,大约阅读时间需要 9 分钟。

D - Cyclic Nacklace HDU - 3746

题目链接:

题目:

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.
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
3aaaabcaabcde
Sample Output
025

题意:至少两个循环节,问你至少要加多少个字母使得字符串都是循环的且循环节大于等于2

思路:利用kmp,求出next数组,其含义为子串前后缀最长相等长度,这样字符串长度-next[n]即可求出一个循环节有多少字母,当next[n]!=0且总长度%循环节字母数=0的话那么

据不需要再加字母了,否则只要输出一个循环节总字母数-next[n]%循环节字母数,减去的那个就是最后一个不是循环节内的已经有的字母数

 

 

1 //  2 // Created by HJYL on 2019/8/15. 3 // 4 #include 
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 const int maxn=1e6+10;18 char str[maxn];19 int nextt[maxn];20 void getnext()21 {22 int n=strlen(str);23 int i=0,j=-1;24 nextt[0]=-1;25 while(i

 

转载于:https://www.cnblogs.com/Vampire6/p/11358563.html

你可能感兴趣的文章
pythone函数基础(14)发送邮件
查看>>
Java的一些好看的
查看>>
Linux 修改文件夹和其中所有文件的权限
查看>>
详解volatile 关键字与内存可见性
查看>>
go 聊天室简单版总结
查看>>
HDU 4258 斜率优化dp
查看>>
Literature review
查看>>
Java 中可变参数
查看>>
PyTorch在64位Windows下的Conda包(转载)
查看>>
php的单元测试,PHPUnit安装
查看>>
CentOS7 设置软件镜像源
查看>>
Java并发编程:并发容器之ConcurrentHashMap
查看>>
Java范例集锦(二)
查看>>
C语言变量和常量
查看>>
LInuxDay8——shell脚本编程基础
查看>>
topcoder 673
查看>>
Java中一些常用的类,包,接口
查看>>
下载特定区域内街景照片数据 | Download Street View Photos within Selected Region
查看>>
StarUML 破解方法
查看>>
C语言结构体
查看>>