hdu 3746 Cyclic Nacklace(kmp最小循环节)
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 ).
- #include <cstdio>
- #include <map>
- #include <iostream>
- #include<cstring>
- #include<bits/stdc++.h>
- #define ll long long int
- #define M 6
- using namespace std;
- inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
- inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
- int moth[]={,,,,,,,,,,,,};
- int dir[][]={, ,, ,-, ,,-};
- int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
- const int inf=0x3f3f3f3f;
- const ll mod=1e9+;
- int nextt[];
- char s[];
- void get_next(){
- nextt[]=; int len=strlen(s);
- for(int i=,j=;i<=len;i++){
- while(j>&&s[i-]!=s[j]) j=nextt[j];
- if(s[j]==s[i-]) j++;
- nextt[i]=j;
- }
- }
- int main(){
- //ios::sync_with_stdio(false);
- int t;
- scanf("%d",&t);
- while(t--){
- scanf("%s",s);
- get_next();
- int len=strlen(s);
- if(nextt[len]==) {
- cout<<len<<endl;continue;
- }
- int t=(len-nextt[len]);
- if(len%t==) cout<<""<<endl;
- else cout<<t-len%t<<endl;
- }
- return ;
- }
hdu 3746 Cyclic Nacklace(kmp最小循环节)的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- hdu 3746 Cyclic Nacklace (KMP求最小循环节)
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...
- HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
- HDU 1358 Period(KMP+最小循环节)题解
思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...
- HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解
思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...
- HDU 3746 Cyclic Nacklace(KMP找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...
随机推荐
- vs快捷键 C#
快速构建构造函数 输入 ctor 然后按 TAB 键 快速构建自动属性 在变量那里,右击鼠标,点“重构”--“封装字段” Visual Studio快捷键 [VS2008/VS2005] ****** ...
- mysql之整型数据int
mysql数据库设计,其中,对于数据性能优化,字段类型考虑很重要,mysql整型bigint.int.mediumint.smallint 和 tinyint的语法介绍,如下:1.bigint 从 - ...
- 配置SQLServer,允许远程连接
需要别人远程你的数据库,首先需要的是在一个局域网内,或者连接的是同一个路由器,接下来就是具体步骤: (一)首先是要检查SQLServer数据库服务器中是否允许远程链接.其具体操作为: (1)打开数据库 ...
- Java中有关Null的9件事(转)
对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常(NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我认 ...
- Hbase存储流程
- web service 异常
1.org/apache/commons/discovery/tools/DiscoverSingleton Exception in thread "main" java.lan ...
- 在阿里云上部署 Postfix
Postfix 可以很方便的在一台机器上部署 smtp 服务,在 centos 上来说的话可以使用: sudo yum install postfix sudo systemctl enable po ...
- tomcat优化实例
———————————————————————————————————— 一.运行模式优化 修改tomcat运行模式为nio<Connector port="80" prot ...
- Atcoder Beginner Contest 118 D-Match Matching(完全背包)
题目链接 题意就是给N根火柴,M个数(M只能是1到9,对应的数字也只能是1到9),只能用这M个出现过的数(但每个数可以随便用多少个,只要火柴够)来拼出一个数字(拼出1,2,3,4,5,6,7,8,9分 ...
- CF 1041 1042整理
终于回来整理了,这两场比赛我也是醉了,第一场-1分,第二场被skip,还是太菜啊qaq CF1041 T1T2过水忽略直接看后面 T3大意:给你一个长度为n的序列a1,a2,a3···an,你需要把这 ...