【KMP+最小循环节】F. Cyclic Nacklace
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F
【题意】
给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前缀循环的字符串。
【思路】
这道题的关键是要理解KMP中的nxt数组什么含义。
nxt[i]就是以i结尾的字符串中前缀和后缀匹配的最长长度。
所以len-nxt[len]就是最小循环节。
如abcdab,nxt[len]就是2,最小循环节就是4(abcd)。
理解了这个这道题就迎刃而解了。
【网上解释】
http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html

【Accepted】
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int n;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
char str[maxn];
int nxt[maxn];
typedef long long ll;
void kmp_pre(int m)
{
int i,j;
j=nxt[]=-;
i=;
while(i<m)
{
while(i<m)
{
while(j!=-&&str[i]!=str[j])
{
j=nxt[j];
}
nxt[++i]=++j;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(nxt,,sizeof(nxt));
scanf("%s",str);
int len=strlen(str);
kmp_pre(len);
//cout<<nxt[len]<<endl;
int ans=len-nxt[len];
if(ans!=len&&len%ans==)
{
printf("0\n");
}
else
{
printf("%d\n",ans-(len-(len/ans)*ans));
}
}
return ;
}
【KMP+最小循环节】F. Cyclic Nacklace的更多相关文章
- HDU3746 Cyclic Nacklace —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 3746 Cyclic Nacklace(kmp最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
- Cyclic Nacklace hdu3746 kmp 最小循环节
题意:给出一段字符串 求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是: 如果nex[len]为0 那么答案显然是补len 否则 答案为循环 ...
- 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 ...
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- HDU - 4333 Revolving Digits(拓展kmp+最小循环节)
1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
随机推荐
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- MySQL+PHP配置 Windows系统IIS版
MySQL+PHP配置 Windows系统IIS版 1.下载 MySQL下载地址:http://dev.mysql.com/downloads/mysql/5.1.html->Windows ( ...
- AJPFX关于modifier总结
修饰符总结 Modifiers 函数修饰符始终在返回值类型之前!!! 变量修饰符始终在变量类型之前!!!---------------------------------- ...
- Redis相关问题收集
问题一.强制关闭Redis快照导致不能持久化 MISCONF Redis is configured to save RDB snapshots, but is currently not able ...
- Myeclipse 6.0代码
import java.util.*; import java.io.*; public class bbs { private static final String LL = "Deco ...
- 5.1点击4个按钮显示相应的div
事件:onclick 属性:display,className 用到for语句,index标记,this当前事件 先清空后附加 <!DOCTYPE html><html>< ...
- JAVA一些错误代码
//算术异常 ArithmeticExecption //空指针异常类 NullPointerException //类型强制转换异常 ClassCastException //数组负下标异常 Neg ...
- 5 Transforms 转移 笔记
5 Transforms 转移 笔记 Transforms Unfortunately, no one can be told what the Matrix is. You have to ...
- SQL——时间戳
mysql 低版本,date.datetime.timestamp 无法精确到毫秒 可以舍弃时间类型字段,用 bigint 来代替,如果用字符串类型代替,还是比较担心排序的时候只是根据第一个字母进行排 ...
- C/C++ 数组、字符串、string
1.定义数组时,数组中元素的个数不能是动态的,不能用变量表示(const变量可以),必须是已知的. 2.引用数组时只能引用数组中某个元素,不能引用整个数组. 3.定义二维数组时,若同时全部初始化,则可 ...