https://cn.vjudge.net/problem/POJ-2406

上面是比赛链接。

题目意思很明确,问最多是多少个子串连接而成的?

这个需要用到KMP,很好的理解KMP的Next数组。Next数组里面存了当匹配失败时应该跳到的位置,所以,最后一个数的部分匹配值 ,判断 n%(n-Next[n])为0 就可以。

Next[len]代表某位字符的部分匹配值,根据部分匹配值可以得到周期为:长度-Next[n]。在最后判断能否被长度整除即可.

#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h> using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++) int Next[MAXSIZE];
char str[MAXSIZE];
void getNext(int n)
{
int i=0,j=-1;
Next[0]=-1;
while(i<n)
{
if(j==-1||str[i]==str[j])
{
j++,i++;
Next[i]=j;
}
else j=Next[j];
}
} int main(int argc,char *argv[])
{
while(scanf("%s",str)!=EOF)
{
if(str[0]=='.') break;
int len=strlen(str);
//printf("len=%d\n",len);
getNext(len);
// for(int i=0;i<=len;i++)
// {
// printf("%d ",Next[i]);
// }
// printf("\n");
if(len%(len-Next[len])==0) printf("%d\n",len/(len-Next[len]));
else printf("1\n");
}
return 0;
} /**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://blog.csdn.net/wr_technology **/
/**************************************************/

Power Strings--KMP的更多相关文章

  1. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  2. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  3. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  4. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  5. UVA10298 Power Strings [KMP]

    题目传送门 Power Strings 格式难调,题面就不放了. 一句话题意,求给定的若干字符串的最短循环节循环次数. 输入样例#1: abcd aaaa ababab . 输出样例#1: 1 4 3 ...

  6. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  7. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  8. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  9. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  10. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

随机推荐

  1. GLSL预定义变量

    GLSL为不同的渲染阶段定义了一些特定的变量.这些预定义(也叫做内置变量)有特定的属性.所有的预定义变量都以gl_开头.用户定义的变量不能以此开头. 下面分类进行介绍. (1)顶点着色器输入 in i ...

  2. [反汇编练习] 160个CrackMe之034

    [反汇编练习] 160个CrackMe之034. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  3. Linux 设备驱动开发 —— platform设备驱动应用实例解析

    前面我们已经学习了platform设备的理论知识Linux 设备驱动开发 —— platform 设备驱动 ,下面将通过一个实例来深入我们的学习. 一.platform 驱动的工作过程 platfor ...

  4. 第五讲_图像识别之图像检测Image Detection

    第五讲_图像识别之图像检测Image Detection 目录 物体检测 ILSVRC竞赛200类(每个图片多个标签):输出类别+Bounding Box(x,y,w,h) PASCAL VOC 20 ...

  5. Opencv 图片边缘检测和最小外接矩形

    #include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/i ...

  6. 醒醒吧少年,只用Cucumber不能帮助你BDD

    转载:http://insights.thoughtworkers.org/bdd/ 引言 在Ruby社区中,测试和BDD一直是被热议的话题,不管是单元测试.集成测试还是功能测试,你总能找到能帮助你的 ...

  7. C 标准库 - <limits.h>

    C 标准库 - <limits.h> 简介 limits.h 头文件决定了各种变量类型的各种属性.定义在该头文件中的宏限制了各种变量类型(比如 char.int 和 long)的值. 这些 ...

  8. Android 扫描Scard卡全部的图片

    这几天为了扫描Scard卡全部的图片的事非常纠结,我原本以为这是一件非常easy的事.可是我发现我错了.网上也没有完整的代码.仅仅是零零碎碎的能扫描单个文件的代码.在今天代码调试通过之后,我认为我有必 ...

  9. java.lang.String中的trim()方法的详细说明(转)

    String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗? 一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱. 首先我直接反编译String ...

  10. HDOJ1160 Fat Mouse&#39;s Speed

    FatMouse's Speed pid=1160">http://acm.hdu.edu.cn/showproblem.php?pid=1160 最长递增子序列问题的一个变体.实际上 ...