Cyclic Nacklace

题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串?

Sample Input
3
aaa
abca
abcde
 
Sample Output
0
2
5
 
思路:要是写过period再来写这道题会发现很简单;就是在getfail()得到f[]之后,贪心地看最后一个字符所能用到的最长后缀长度为多少?而这个后缀长度就是n - n%len;(n为字符串长度,len为循环节的长度)。当然事先要看是否本身就是一个循环串;时间复杂度O(n)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5 + ;
char p[N];
int f[N];
void getfail(char *p,int *f)
{
f[] = f[] = ;
int n = strlen(p);
for(int i = ;i < n;i++){
int j = f[i];
if(j && p[i] != p[j]) j = f[j];
f[i+] = (p[i] == p[j] ?j+:);// i+1会递推到第n位
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%s", p);
getfail(p,f);
int n = strlen(p),len = n - f[n];
if(f[n] && n%len == ) puts("");//本身就是周期串
else{
printf("%d\n",len-n%len);
}
}
}

hdu 3746 Cyclic Nacklace KMP循环节的更多相关文章

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

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

  2. HDU 3746 Cyclic Nacklace (KMP找循环节)

    题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...

  3. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

  4. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  5. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  6. HDU 3746 Cyclic Nacklace KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...

  7. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. HDU 3746 Cyclic Nacklace (用kmp求循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. HDU 3746 Cyclic Nacklace(KMP找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...

随机推荐

  1. 再探ASP.NET 5(转载)

    就在最近一段时间,微软又有大动作了,在IDE方面除了给我们发布了Viausl Studio 2013 社区版还发布了全新的Visual Studio 2015 Preview. Visual Stud ...

  2. Android市场官方的统计信息

    做Android应用和游戏,避免不了的要了解市面上的各种android设备的信息,以最大程度的兼容更多的设备. Android市场会定期发布统计信息,包括SDK版本,屏幕大小和分辨率,OpenGL E ...

  3. Fast portable non-blocking network programming with Libevent--转

    Learning Libevent Chapter 0: About this document Chapter 1: A tiny introduction to asynchronous IO. ...

  4. SerialPort基本小例

    SerialPort是用于串口通信的控件与VB6中的MSCOMM控件相似,使用很方便... vb.net CodeImports System.IO.PortsImports System.TextP ...

  5. c/c++将整数转换为字符串

    #include <iostream> using namespace std; int main(int argc, char **argv) { ; iint i,j; ],e[]; ...

  6. About Webkit

    http://blog.csdn.net/spacetiller/article/details/5784461 一 . WebKit 简介 Webkit 是一个开放源代码的浏览器引擎 (web br ...

  7. Cstring到string

    要利用mfc,然后接受一个图片. imread只能读const string& filename 的东西. imread 原型: CV_EXPORTS_W Mat imread( ); 它的参 ...

  8. git svn 简易同时使用

    这个方法适合于新的一个git 仓库.假如你使用的git 是最新版本,git本身提供了 git svn命令. 1. 进入一个空的目录,初始化一个空的git仓库: git svn init svn://x ...

  9. php的递归函数

    递归函数,就是在函数体内调用自身 例子: <?php function repayment($number){  if ($number<10){   echo $number." ...

  10. 【转】Oracle - 数据库的实例、表空间、用户、表之间关系

    [转]Oracle - 数据库的实例.表空间.用户.表之间关系 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机 ...