Cyclic Nacklace

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

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
 
题意:最少需要加入多少个字符才能让整个字符串完全匹配
1、先找出这个字符串中循环次数最多的字符串(长度为n),并求出循环次数m和这个字符串长度len,输出len-m*n就行了
//HD3746
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = ;
int i, n, m, t, sum;
char T[N], P[N];//P[]是需要比较的字符串,T[]是被比较的字符串
int next1[N];
void getnext(char *P, int *next1)
{
int j = ;
int k = -;
next1[] = -;
while (j <= m)
{
if (k == - || P[j] == P[k])
{
next1[++j] = ++k;
}
else
{
k = next1[k];
}
}
}
int main()
{
cin >> t;
while (t--)
{
scanf("%s", P);
m = strlen(P);
getnext(P, next1);
n = m - next1[m];
if (n != m && m%n == )
cout << "" << endl;
else
cout << n - next1[m] % n << endl;
}
//system("pause");
return ;
}

相似题目https://www.cnblogs.com/-citywall123/p/10034261.html

hdu3746 KMP-next数组的应用的更多相关文章

  1. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  2. hdu3746 KMP的next数组应用,求项链首尾项链循环

    题意:       给你一个项链,问你最少加多少个珠子能满足整个项链是一个循环的项链(首尾相连) 思路:      KMP的简单应用只要了解next数组的意义就好说了,下面总结下  next在循环方面 ...

  3. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  4. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  5. kmp next数组的理解(挺好的一篇文章 ,原来kmp最初的next是这样的啊,很好理解)

    KMP算法的next[]数组通俗解释   我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我 ...

  6. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  7. hdu-3746(kmp)

    题意:给你一个字符串,问你至少增添几个字符可以把这个字符串变成一个循环字符串(ababa的循环节是ab,不是aba): 解题思路:利用kmp中的next数组,首先在这样求next的数组的代码里: vo ...

  8. 【HDU - 5442】Favorite Donut 【最大表示法+KMP/后缀数组】

    题意 给出一个长度为n的环状由小写字母组成的序列,请找出从何处断开,顺时针还是逆时针,使得字典序最大.如果两个字符串的字典序一样大,那么它会选择下下标最小的那个.如果某个点顺时针逆时针产生的字典序大小 ...

  9. POJ2406Power Strings (最小循环节)(KMP||后缀数组)

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  10. [NOI2014][bzoj3670] 动物园 [kmp+next数组应用]

    题面 传送门 思路 首先,这题最好的一个地方,在于它给出的关于$next$的讲解实在是妙极......甚至可以说我的kmp是过了这道题以后才脱胎换骨的 然后是正文: 如何求$num$数组? 这道题的输 ...

随机推荐

  1. import configparser

  2. 2018网络预选赛 徐州G 线段树

    线段树,假设求(x1,y1)点的贡献,就找所有比该点出现时间晚,且x坐标大于x1的点中y最大的,贡献就是Y-y1,由于题目条件限制,不可能有x坐标大于(x1,y1)且y坐标大于y1的点,所以贡献肯定为 ...

  3. PHP trim() 函数

    定义和用法 trim() 函数从字符串的两端删除空白字符和其他预定义字符. 语法 trim(string,charlist) 参数 描述 string 必需.规定要检查的字符串. charlist 可 ...

  4. cocos2d-js 浏览器与JSB内存管理机制的不同

    写这边文章的主要目的是为了理解使用cocos3d-js开发app时,浏览器调试与真机情况不一致的原因 一.浏览器中内存管理机制 HTML5版本运行时,整个游戏只存在JS脚本与一些必要的资源文件,这时候 ...

  5. 3dsmax里面的喷射器spray和超级喷射器superspray是个什么东西

    以前搜索过粒子系统的一些资料,最早有一篇什么论文里提到一种方法可以用计算机模拟一些看起来数目比较多的一些效果,比如 很多雨滴.下雪天的雪花,作者把这个东西叫particle system,就是粒子系统 ...

  6. 高性能MySQL笔记-第5章Indexing for High Performance-001B-Tree indexes(B+Tree)

    一. 1.什么是B-Tree indexes? The general idea of a B-Tree is that all the values are stored in order, and ...

  7. SDUT 3363 数据结构实验之图论七:驴友计划

    数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...

  8. [学习笔记]_exit和exit深入理解

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...

  9. [译]Javascript中的闭包(closures)

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  10. easyUI 展开DataGrid里面的行显示详细信息

    http://blog.csdn.net/yanghongchang_/article/details/7854156原著 datagrid 可以改变它的view(视图)去显示不同的效果.使用详细视图 ...