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. ubuntu navicat for mysql破解

    ubuntu navicat for mysql破解 ubuntu navicat for mysql只能试用14天. 破解方法:rm -rf /home/cxg/.navicat64/

  2. 【java】spring项目中 对entity进行本类间的克隆

    方法1: [使用spring自带BeanUtils实现克隆] [要求:需要被克隆的类实现Cloneable接口并且重写clone()方法] >例子: >>实体: package co ...

  3. GridControl 校验输入单元格格式 z

    // 对应的校验行事件,ValidateRow private void gv_FeeItem_ValidateRow(object sender, DevExpress.XtraGrid.Views ...

  4. iOS -- SKKeyframeSequence类

    SKKeyframeSequence类 继承自 NSObject 符合 NSCodingNSCopyingNSObject 框架  /System/Library/Frameworks/SpriteK ...

  5. JavaScript中的Math方法演示

    <html> <head> <script type="text/javascript"> var num = 12.4; alert(Math ...

  6. redis主从连接不成功错误

    redis主从连接不成功错误 学习了:https://blog.csdn.net/wzqzhq/article/details/64919133 需要增加 masterauth  password.. ...

  7. xgboost的SparkWithDataFrame版本实现

    再xgboost的源码中有xgboost的SparkWithDataFrame的实现,如下:https://github.com/dmlc/xgboost/tree/master/jvm-packag ...

  8. vue class绑定 组件

    当在一个自定义组件上使用 class 属性时,这些类将被添加到该组件的根元素上面.这个元素上已经存在的类不会被覆盖. 例如,如果你声明了这个组件: Vue.component('my-componen ...

  9. Odoo calendar 提醒器

    Odoo calendar 提供了一个提醒功能,它包含邮件通知以及web client弹窗功能     创建日历事件的时候,可以设置提醒器     Meeting [ calendar.event ] ...

  10. 最长连续序列(Longest Consecutive Sequence)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...