HDU3746 Cyclic Nacklace —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-3746
Cyclic Nacklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10985 Accepted Submission(s): 4692
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.
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 ).
aaa
abca
abcde
2
5
题解:
求出最小循环节,然后求出最少需要补多少个字符使得其长度能被循环节整除。
注意:当循环节为自身时,需要再补全一份自己。因为题目要求了至少有两个循环节。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", x);
int len = strlen(x);
get_next(x, len);
int r = len-Next[len]; //找到最小循环节
if(len!=r && len%r==) //如果最小循环节不等于自己,且除得尽,则不需要补全
printf("0\n");
else //否则,补全使其循环
printf("%d\n", r-len%r);
}
}
HDU3746 Cyclic Nacklace —— KMP 最小循环节的更多相关文章
- hdu 3746 Cyclic Nacklace(kmp最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
- Cyclic Nacklace hdu3746 kmp 最小循环节
题意:给出一段字符串 求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是: 如果nex[len]为0 那么答案显然是补len 否则 答案为循环 ...
- 【KMP+最小循环节】F. Cyclic Nacklace
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F [题意] 给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前 ...
- hdu3746(kmp最小循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 题意:问在一个字符串末尾加上多少个字符能使得这的字符串首尾相连后能够循环 题解:就是利用next ...
- HDU-3746-Cyclic Nacklace(KMP,循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- UVAlive 3026 KMP 最小循环节
KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...
随机推荐
- UVa10234 Race
递推,设有i个人排在第一名,剩下的人排在后面,方案有f[i]种,则f[i]=sum(c[n][i]*f[n-i]) 1<=i<=n /*by SilverN*/ #include<a ...
- python 之文件操作
一.文件基本操作 1.文件的打开 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作 文件句柄 = open('文件路径', '模式') 2. ...
- a href="javascript:void(0)"
JavaScript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. void 操作符用法格式如下: 1. javascript:void (expression) 2. javas ...
- 什么是Hadoop?什么是HDFS?
[学习笔记] 什么是Hadoop?什么是HDFS?马 克-to-win @ 马克java社区:Hadoop是Apache基金会开发的一个分布式系统基础架构.比如前面我们接触的Spring就是一个开发应 ...
- mysql大数据量分页查询优化
参考文章:https://www.dexcoder.com/selfly/article/293 Mysql的分页查询十分简单,但是当数据量大的时候一般的分页就吃不消了. 传统分页查询:SELECT ...
- 取汉子拼音首字母的VB.Net方法
'/ <summary> '/ 获得一个字符串的汉语拼音码 '/ </summary> '/ <param name="strText">字符串 ...
- scapy在wlan中的应用
Scapy 又是scapy,这是python的一个网络编程方面的库,它在wlan中也有很强大的应用.一般我们买块网卡,然后aircrack-ng套件爆破一下邻居的密码,其实我们可以用scapy写一些有 ...
- 公司hadoop客户端试用
今天用了一下公司的hadoop客户端,从外面下载的客户端不能用,只能用这个wiki里面提供的:link 装在了 tc-cm-201511novam12x12n0.tc 目录 /home/work/vi ...
- 关于android 使用bitmap的OOM心得和解决方式
android开发,从2010年開始学习到如今的独立完毕一个app,这漫长的四年,已经经历了非常多次bug的折磨.无数次的加班训练.然而,自以为自己已经比較了解android了,却近期在一个项目上.由 ...
- innodb 修改表共享空间为独立空间
最近在优化mysql innodb存储引擎,准备把共享表空间转换成独立表空间.刚开始的没考虑这么多,过段时间又要推广,所以优化一下,看看效果如何.说一个转换过程. 1,查看一下是共享表空间,还是独立表 ...