原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3746

一:原题内容

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

二:分析理解

给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数。

例子:

abcabc 已经循环2次,添加数为0

abcac 没有循环2次,添加字符abcac。数目为5.

abcabcab 已经循环过2次,但第三次不完整,需要添加数为1

三:AC代码

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include<iostream>
#include<string.h>using namespace std;char ch[100010] = "#";
int nextval[100010];void GetNextval()
{int len = strlen(ch + 1);int i = 1, j = 0;nextval[1] = 0;while (i <= len){if (j == 0 || ch[i] == ch[j]){i++; j++;if (ch[i] != ch[j])nextval[i] = j;elsenextval[i] = nextval[j];}elsej = nextval[j];}
}int main()
{int N;scanf("%d", &N);while (N--){scanf("%s", ch + 1);//------>>>>>int len = strlen(ch + 1);GetNextval();int cycleLen = len - nextval[len + 1] + 1;//循环长度if (len != cycleLen&&len%cycleLen == 0)printf("0\n");elseprintf("%d\n", cycleLen - len%cycleLen);}return 0;
}

此题还有一份使用未优化的next数组来做的,参见 http://blog.csdn.net/niushuai666/article/details/6965517

hdu3746 Cyclic Nacklace--KMP相关推荐

  1. HDU - 3746 Cyclic Nacklace(KMP的next数组判循环节)

    题目链接:点击查看 题目大意:现在规定想要制作一串珍珠手链,需要用到两段一模一样的字符串首位相接而成,现在给定一个字符串,问最少需要添加几个珍珠才能满足条件 题目分析:一开始以为是个简单的模拟题,但后 ...

  2. HDU - Cyclic Nacklace(KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  3. 【HDU - 3746 】Cyclic Nacklace (KMP,最小循环节问题)

    题干: CC always becomes very depressed at the end of this month, he has checked his credit card yester ...

  4. @hdu - 3746@ Cyclic Nacklace

    目录 @description@ @solution@ @code@ @details@ @description@ 给你一个长度为 n 的由小写字母组成的字符串,让你在末尾增加尽量少的字母,使它变为 ...

  5. 字符串周期--hdu 3746 Cyclic Nacklace

    通过这题得出了一个很重要的结论,在用kmp算法求出next数组后,len-next[len]的值就是该字符串的最小循环节,该字符串的其他循环节都是它的倍数,如果len恰好是len-next[len]的 ...

  6. hdu 3746 Cyclic Nacklace

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 思路:KMP中Next数组的应用,求出最小的循环节,题目的意思是只能在字符串的后面上添加新的字符 ...

  7. KMP Trie 例题讲解

    文章目录 HDU 4763 Theme Section 题意: 题解: 代码: POJ 3630 Phone List 题意: 题解: 代码: HDU 3746 Cyclic Nacklace 题意: ...

  8. 字符串处理 —— 单模式匹配

    [问题形式] 文本(Text):长度为 n 的数组 T[1..n] 模式(Pattern):一个长度为 m 且 m≤n 的数组 P[1..m] 有效位移/匹配点(Valid Shift):0≤s≤n- ...

  9. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

最新文章

  1. 走近酷点KoolPoint,让我们深度挖掘网管软件的设计细节
  2. ML之模型文件:机器学习、深度学习中常见的模型文件(.h5、.keras)简介、h5模型文件下载集锦、使用方法之详细攻略
  3. DL之RNN:循环神经网络RNN的简介、应用、经典案例之详细攻略
  4. 计算图像相似度——《Python也可以》之一(转)
  5. 产品入门十——互联网产品的交互设计
  6. NBU计算机专业期末考试记录
  7. java 进制转换算法_算法笔记_033:十六进制转八进制(Java)
  8. oracle11g 时间失效,关于oracle11g RAC 的CTSS与ntp时间同步的疑问
  9. python400集视频教程-微软官方出品的400集Python精品视频教程,这正是我们急需的!...
  10. 336 Palindrome Pairs 回文对
  11. iphone12屏幕尺寸
  12. 机载激光雷达原理与应用科普(一)
  13. win的反义词_小学英语常用近义词、反义词、同音词汇总 ~~~ 赶紧转给孩子
  14. mysql 如何 导入txt文件_mysql中导入txt文件
  15. C语言指针用法完善篇
  16. SQLServer2008 SQLSERVER服务远程过程调用失败解决方案
  17. 微信墙html5,微信墙怎么做?微信墙制作流程介绍
  18. 建立时间和保持时间概念
  19. Oracle Sequence创建与使用
  20. n270运行Linux,古董笔记本电脑拯救记——安装小型操作系统

热门文章

  1. linux fsck命令,linux fsck 命令详解
  2. 嵌入式工程师的灵魂三问
  3. 9.3 Qt实现电子闹钟
  4. 一个宝妈的兼职赚钱方法,一个很不错的吸引流量的方法
  5. GPT转MBR分区格式图文教程
  6. 基于vue3 element plus form 表单的二次封装
  7. 权限类型和权限分配分析
  8. cnbc for android,cnbc新闻app下载 安卓
  9. 3-env 文件与环境设置
  10. java抽象类中必须有抽象方法吗_011 抽象类必须要有抽象方法吗?