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. Java分布式IP限流和防止恶意IP攻击方案

    前言 限流是分布式系统设计中经常提到的概念,在某些要求不严格的场景下,使用Guava RateLimiter就可以满足.但是Guava RateLimiter只能应用于单进程,多进程间协同控制便无能为 ...

  2. Python第二章-变量和数据类型

    变量和数据类型 一.什么是变量,常量 思考:程序执行指的是什么? 对数据进行存储处理和计算,最终获得结果,这是程序执行的本质. 变量的概念和在数学中的变量的概念一样的,只是在计算机程序中,变量不仅可以 ...

  3. UNIX环境高级编程——TCP/IP网络编程 常用网络信息检索函数

    UNIX环境高级编程——TCP/IP网络编程   常用网络信息检索函数 gethostname()   getppername()   getsockname()   gethostbyname() ...

  4. 解决使用requests_html模块,html.render()下载chromium报错、速度慢问题

    来源:https://www.cnblogs.com/xiaoaiyiwan/p/10776493.html 稍作修改 1.第一步,代码如下: from requests_html import HT ...

  5. 使用scikit-learn解决文本多分类问题(附python演练)

    来源 | TowardsDataScience 译者 | Revolver 在我们的商业世界中,存在着许多需要对文本进行分类的情况.例如,新闻报道通常按主题进行组织; 内容或产品通常需要按类别打上标签 ...

  6. Vue2.0 -- 钩子函数/ 过度属性/常用指令/以及Vue-resoure发送请求

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. SG函数(斐波那契博弈) Fibonacci again and again

    https://zhuanlan.zhihu.com/p/53948422 HDU - 1848 将这篇文章认真的看了一遍 ,虽然不是很懂 ,但是脑子里有了一个模型,链接里的图 (看的顺序 是 0,1 ...

  8. nginx使用手册+基本原理+优缺点

    一.nginx优点 1.反向代理 1.正向代理: 客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原 ...

  9. 第 k 小的数

    一.寻找两个有序数组的中位数 1.1 问题描述 给定两个大小为 m 和 n 的不同时为空的有序数组 nums1 和 nums2.找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m ...

  10. 【译】Java SE 14 Hotspot 虚拟机垃圾回收调优指南

    原文链接:HotSpot Virtual Machine Garbage Collection Tuning Guide,基于Java SE 14. 本文主要包括以下内容: 优化目标与策略(Ergon ...