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的更多相关文章

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

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

  2. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  3. Cyclic Nacklace hdu3746 kmp 最小循环节

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

  4. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  5. UVAlive 3026 KMP 最小循环节

    KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...

  6. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  7. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  8. HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

    1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...

  9. [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...

随机推荐

  1. Android内存堆上限Android的缺省值是16M(某些机型是24M)

    转自: http://www.cnblogs.com/jacktu/archive/2010/12/30/1921475.html 大家都知道Android的上层应用是基于 Dalvik Virtua ...

  2. http缓存之lastModified和etag

    1.cache-control 访问资源 首次访问页面时间:2018.2.1  9:56  (当前时间=GMT时间+8h) 缓存时长max-age:1 day Expire缓存失效时间:2018.2. ...

  3. 外文翻译 《How we decide》赛场上的四分卫 第二节

    本书导言翻译 本章第一节 "决定是如何做出来的",关于意识最神秘的问题之一.尽管我们时刻做着决定,但是我们没有感觉到大脑内部的一系列有关进程.NFL球探挑选候选球员的评分表中,决策 ...

  4. leetcode:single-number-ii(Java位运算)

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  5. 使用Jenkins进行android项目的自动构建(3)

    建立Jenkins项目 1. “新增作业”->填写作业名称->选择“建置 Maven 2 或 3 專案”->OK.新增成功后会进入“組態設定”,暂时先保留默认值,稍后再进行设定. 2 ...

  6. reStructuredText学习

    reStructuredText学习====================2015年4月1日 学习的最好方法就是尽快动手开始.不断迭代,不断完善. reStructuredText学习v0.1版本. ...

  7. 【译】x86程序员手册39-10.3切换到保护模式

    10.3 Switching to Protected Mode  切换到保护模式 Setting the PE bit of the MSW in CR0 causes the 80386 to b ...

  8. 浅谈kernel的结构图及生成过程-----(1)

    当今,我们身边如此多的服务器,工作站都运行着linux,因此也有不少的朋友想了解linux内的核心机理.但是由于kernel过于庞大,以致让一些朋友望而却步.(我在大二的时候也有过此经历,当时看到一些 ...

  9. swift class extension 与继承

    1.扩展中无法继承重写已有函数,不能添加函数. Extensions can add new functionality to a type, but they cannot override exi ...

  10. 浮动qq客服备份代码

    <div class="main-im"> <div id="open_im" class="open-im"> & ...