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
题目大意:就是给你一个字符串,让你判断最少加上多少个字符才能使其完美匹配
用到的知识:KMP,还有就是最小循环长度等于总长度减去next[n-1](从0计数)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1E6+;
char arr[N];
ll nxt[N];
//abcdefabcdefab
//总长度n减去nxt[n-1]就是循环节的最短长度
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",arr);
ll n=strlen(arr);
for(ll i=;i<n;i++){
ll j=nxt[i-];
while(arr[i]!=arr[j]&&j>) j=nxt[j-];
if(arr[i]==arr[j]) j++;
nxt[i]=j;
}
int len=n-nxt[n-];
if(len==n) printf("%d\n",len);
else if(n%len==){
puts("");
}
else printf("%d\n",len-n%len);
}
return ;
}
 

Cyclic Nacklace 杭电3746的更多相关文章

  1. (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746

    http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...

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

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

  3. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  4. HDU 3746:Cyclic Nacklace

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

  5. hdu 3746 Cyclic Nacklace KMP循环节

    Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...

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

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

  7. HDU 3746:Cyclic Nacklace(KMP循环节)

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

  8. HDU 3746 Cyclic Nacklace (用kmp求循环节)

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

  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. thinkphp 路径 (纯转)

    TP中有不少路径的便捷使用方法,比如模板中使用的__URL__,__ACTION__等,如果你对这些路径不是很明白,用起来说不定就会有这样或那样的问题,抑或出了错也不知道怎么改,现在我们看一下这些路径 ...

  2. TensorFlow 安装官方教程:Ubuntu 安装,Mac OS X 安装,Windows 安装

    从我的使用体验来看  Ubuntu 是最好的, Mac 没有显卡,后期跑大项目比较鸡肋,Windows 安装各种依赖各种坑.Ubuntu 安装 TensorFlow 方便,后面安装  TensorFl ...

  3. Github标星过万,Python新手100天学习计划,这次再学不会算我输!

      作为目前最火也是最实用的编程语言,Python不仅是新手入门程序界的首选,也逐渐成为了从大厂到小厂,招牌需求list的必要一条. 当然,学Python这件事情,你可能也和文摘菌一样,已经下了一百次 ...

  4. ADO.NET 的使用(二)

    一.本篇主要讲ADO.NET的核心DataSet DataSet里面有个DataTable,DataTable还有个对应的DataView 首先呢,假设数据库的数据 id name mob 100 张 ...

  5. Linux/CentOS7搭建Anaconda运行环境

    1. 安装miniconda 下载并安装miniconda到$HOME/.miniconda/ #从官网下载 wget -c "https://repo.anaconda.com/minic ...

  6. ArrayList 扩容 和 Vector

    public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[siz ...

  7. n次方

    1.问题描述 计算 an 2.算法分析 先将 n 变一变,寻找新的计算路径.预处理就是变治法的根本. 如果单纯循环执行 n 次相乘,那么时间复杂度为 O(n).可以利用二进制幂大大改进效率. 主要思路 ...

  8. File.Create(path)未关闭遇到的一点点问题

    本人老菜鸟一枚,不是因为偶是菜鸟中的老手,而是偶是老了但是还是很菜的鸟╮(╯▽╰)╭,不过打今儿起偶想要腾飞…… 今天写文本文件编辑类时遇到一个小问题,下面先将问题描述一下: 1.写文本文件时都会习惯 ...

  9. python文件封装成*.exe

    python文件封装成*.exe文件(单文件和多文件) 环境:win10 64位 python3.7 原文: https://www.cnblogs.com/jackzz/p/9431923.html ...

  10. 并发——详细介绍CAS机制

    一.前言   今天花了点时间了解了一下JDK1.8中ConcurrentHashMap的实现,发现它实现的主要思想就是依赖于CAS机制.CAS机制是并发中比较重要的一个概念,所以今天这篇博客就来详细介 ...