Cyclic Nacklace

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

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
 
Author
possessor WC
 
Source
 
Recommend
lcy
 

题意:

找一个字符串的最小循环节。问最后不构成循环节的那段要加多少个字符才能构成循环节。

思路:

一个字符串的最小循环节为$len-nxt[len]$

【见http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html

分三种情况:1、如果$nxt[len]$为$0$,输出$len$。 2、如果$len$整除循环节,输出为$0$。 3、否则输出为$len-nxt[len]-len%(len-nxt[len])$

【题目necklace拼错了吧....】

 #include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e5 + ;
int n, t;
char str[maxn];
int nxt[maxn]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == s[k]){
++k;++j;
if(s[j] != s[k]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} int main()
{
scanf("%d", &t);
while(t--){
memset(nxt, , sizeof(nxt));
scanf("%s", str);
int len = strlen(str);
getnxt(str); if(nxt[len] == ){
printf("%d\n", len);
}
else{
int ans = len - nxt[len];
if(len % ans == ){
printf("0\n");
}
else{
printf("%d\n", ans - len % ans);
}
}
}
return ;
}

hdu3746 Cyclic Nacklace【nxt数组应用】【最小循环节】的更多相关文章

  1. hdoj3746(kmp算法的nex数组求最小循环节)

    题目链接:https://vjudge.net/problem/HDU-3746 题意:给定一个字符串,问最少在两端添加多少元素使得整个字符串是呈周期性的. 思路: 应用到kmp中nex数组的性质,数 ...

  2. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  3. poj 2185 Milking Grid(next数组求最小循环节)

    题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...

  4. next数组求最小循环节

    1.kmp产生的next数组: 最小循环节(长度)=len-next[len]; 证明: ----------------------- ----------------------- k    m ...

  5. Cyclic Nacklace - HDU 3746(next求循环节)

    题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next ...

  6. poj2185(kmp算法next数组求最小循环节,思维)

    题目链接:https://vjudge.net/problem/POJ-2185 题意:给定由大写字母组成的r×c矩阵,求最小子矩阵使得该子矩阵能组成这个大矩阵,但并不要求小矩阵刚好组成大矩阵,即边界 ...

  7. HDU3746 Cyclic Nacklace —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. HDU-3746 Cyclic Nacklace 字符串匹配 KMP算法 求最小循环节

    题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...

  9. Cyclic Nacklace hdu3746 kmp 最小循环节

    题意:给出一段字符串  求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是:  如果nex[len]为0  那么答案显然是补len 否则  答案为循环 ...

随机推荐

  1. PL/SQL学习笔记之函数

    一:函数 函数与过程的最大不同就是,函数有返回值.适用于需要返回结果的场景. 二:创建函数 CREATE [OR REPLACE] FUNCTION function_name [(parameter ...

  2. Ubuntu中安装和配置 Java JDK,并卸载自带OpenJDK(以Ubuntu 14.04为例)

    1.下载jdk-7u67-linux-x64.tar.gz 2.用ftp客户端工具filezilla上传到ubuntu的合适文件夹.如果如果不能上传到指定文件夹可能是文件夹权限不足,修改文件夹可执行权 ...

  3. swift3 单例写法

    import UIKit class SingleOnce { // 单例 static let shared = SingleOnce.init() private init(){} // 其他方法 ...

  4. PC端和移动端在前端开发上的一些区别,前端里移动端到底比pc端多哪些知识

    (1)———————— 前端里移动端到底比pc端多哪些知识,为啥面试时好多公司都问h5水平如何?我做过几年的web前端开发,就简单谈谈自己的感受吧.首先来看看PC端和移动端在前端开发上的一些区别: ( ...

  5. Axure RP for Mac(网站交互式原型设计工具)破解版安装

    1.软件简介    Axure RP 是 macOS 系统上一款最知名和最强大的原型设计工具,增加了大量新的特性,如应用多个动画,并同一时间运行一个小部件,如褪色,同时移动等,而且具有全新的图标和界面 ...

  6. 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api ...

  7. FFMPEG中关于ts流的时长估计的实现(转)

    最近在做H.265 编码,原本只是做编码器的实现,但客户项目涉及到ts的封装,搞得我不得不配合了解点ts方面的东西.下面技术文档不错,转一下. ts流中的时间估计 我们知道ts流中是没有时间信息的,我 ...

  8. Socket网络编程--聊天程序(8)

    上一节已经完成了对用户的身份验证了,既然有了验证,那么接下来就能对不同的客户端进行区分了,所以这一节讲实现私聊功能.就是通过服务器对客户端的数据进行转发到特定的用户上, 实现私聊功能的聊天程序 实现的 ...

  9. 自己实现字符串转整数(不使用JDK的字符串转整数的方法)

    [需求]: (1)如果输入的字符串为null,为空,或者不是数字,抛出异常: (2)如果输入的数字超出int的最大值或最小值,抛出异常: (3)输入的数字允许以+或-号开头,但如果输入的字符串只有&q ...

  10. elk问题,求教各位大虾!

    [filebeat --> kafka --> logstash-->MongoDB|磁盘]架构进行日志收集 但是当logstash写入MongoDB有延迟,然后正常之后,会导致lo ...