Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11516    Accepted Submission(s):
4913

Problem Description
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.

 
Input
The 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 ).
 
Output
For 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
 
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
char a[];
int nxt[];
void get_next(int l)
{
int i = , j = -;
nxt[] = -;
while (i != l)
{
if (j == - || a[i] == a[j])
{
i++;
j++;
nxt[i] = j;
}
else
j = nxt[j];
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%s", a);//居然用cin>>a就wa了
int m = strlen(a);
get_next(m);
int n = m - nxt[m];//代表循环节的长度
if (n != m && m%n == )printf("0\n");//如果可以多次循环
else printf("%d\n", n - nxt[m] % n);//取余的作用:abcab,去掉abc
}
return ;
}

HDU 3746 Cyclic Nacklace (用kmp求循环节)的更多相关文章

  1. HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解

    思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...

  2. hdoj 3746 Cyclic Nacklace【KMP求在结尾加上多少个字符可以使字符串至少有两次循环】

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. HDU 3746 Cyclic Nacklace(KMP找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...

  4. hdu 3746 Cyclic Nacklace(KMP)

    题意: 求最少需要在后面补几个字符能凑成两个循环. 分析: 最小循环节的应用,i-next[i]为最小循环节. #include <map> #include <set> #i ...

  5. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  6. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  7. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  8. 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

    Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...

  9. HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. Python isspace()方法--转载

    描述 Python isspace() 方法检测字符串是否只由空格组成. 语法 isspace()方法语法: str.isspace() 参数 无. 返回值 如果字符串中只包含空格,则返回 True, ...

  2. Mongo配置基础

    数据库也是一种服务,数据库的本质也是一个文件,所以说我们把文件存入text和存入数据库的本质是一样的,只是数据库的格式化的删除和添加. 分为四部分, mongo的启动详解 导入导出,运行时备份 Fsy ...

  3. 【Demo】CSS3 动画 加载进度条

    实例结果图: 完整代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. PowerDesigner16工具学习笔记-建立BPM

    根据不同用途,BPM分为分析性(Analysis).执行型(Executable)和协作型(Collaborative) BPM的类型 业务流程语言 描述  分析型  Analysis  提供流程层次 ...

  5. 【hive】数据仓库层次设计

    转载 https://www.jianshu.com/p/849db358ec61

  6. 蓝桥杯练习系统历届试题 带分数 dfs

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...

  7. 《转》快速导出SSRS之RDL文件

    select name,[path],cast(cast(content AS varbinary(max)) as xml) as RDLDef from dbo.[Catalog] where t ...

  8. L183 Chinese company unveils first satellite for free WiFi

    A Chinese internet technology company unveiled the first satellite in a constellation plan to provid ...

  9. PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/phalcon.so' - /usr/lib64/php/mod

    这个警告可能是,扩展在php.d里面加载了一遍,然后又在php.ini里写了一遍导致的

  10. 滑动平均线的notebook画法

    滑动平均线,本程序解决了如何在matplotlib中使用中文显示,环境python2.7 最好使用 anaconda 环境使用sns似使得图片更加美观,不多说,上代码 import tushare a ...