题目:求一个串的最大的循环次数。

分析:dp。KMP,字符串。这里利用KMP算法。

KMP的next函数是跳跃到近期的串的递归结构位置(串元素取值0 ~ len-1);

由KMP过程可知:

假设存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配;

则S[next[len] ~ len-1]就是循环节(且最小)。否则next[len]为0;

因此,最大循环次数为len/(len-next[len])。最小循环节为S[next[len] ~ len-1]。

说明:强大的KMP(⊙_⊙)。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio> using namespace std; char str[1000004];
int next[1000004]; void getnext( char *T, int L )
{
next[0] = -1;
int i = 0,j = -1;
while ( i < L ) {
if ( j == -1 || T[i] == T[j] ) {
i ++; j ++;
next[i] = j;
}else j = next[j];
}
} int main()
{
while ( scanf("%s",str) && strcmp( str, "." ) ) {
int len = strlen(str);
getnext( str, len );
printf("%d\n",len/(len-next[len]));
}
return 0;
}

UVa 10298 - Power Strings的更多相关文章

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

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

  2. UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

    题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...

  3. POJ 2406 Power Strings (KMP)

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

  4. POJ 2406 Power Strings

    F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  5. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  6. POJ 2406:Power Strings

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41252   Accepted: 17152 D ...

  7. E - Power Strings,求最小周期串

    E - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  8. poj 2406 Power Strings kmp算法

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

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

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

随机推荐

  1. Back Track 5 之 网络踩点

    DNS记录探测 dnsenum 针对NDS信息收集的工具 格式: ./dnsenum.pl dbsserver (域名) 请原谅我拿freestu.net这个学校团委的域名做的测试,求不黑!! dns ...

  2. kettle利用参数遍历执行指定目录下的所有对象

    使用kettle设计ETL设计完成后,我们就需要按照我们业务的需要对我们设计好的ETL程序,ktr或者kjb进行调度,以实现定时定点的数据抽取,或者说句转换工作,我们如何实现调度呢? 场景:在/wor ...

  3. Jmeter-Maven-Plugin高级应用:Proxy Configuration

    Proxy Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Configuratio ...

  4. js的正则匹配 和 blur

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js&qu ...

  5. 火狐浏览器Firefox如何使用插件,火狐有哪些好用的插件

    1 CoorPreviews 不打开网页链接预览该网页的内容. 预览如图所示: 点击关闭旁边的钉子可以让该窗口保持开着并且浏览速度加快.这对于快速浏览图片时非常有用. 2 FoxTab 3D方式预览网 ...

  6. 使用第三方控件DotNetBar来美化程序

    VS的控件确实有点丑陋,需要美化一下.我最先接触的就是DotNetBar,一直用它,一般都还稳定.下面简单地讲解一下使用方法 1. 下载破解版DotNetBar 10版本:http://www.cr1 ...

  7. libcurl 接口调用方式

    http://hi.baidu.com/tracyu1026/item/bb6d5def4292b10b570f1d48 libcurl提供了一组C语言API函数直接调用.首先需要提到的两个函数就是c ...

  8. java面试第六天

    集合:保存多个其他对象的对象,不能保存简单类型 List:有序(存放元素的顺序),可重复的集合 ArrayList:实质就是一个会自动增长的数组 查询效率比较高,增删的效率比较低,适用于查询比较频繁, ...

  9. caffe 代码阅读笔记1

    首先查看caffe.cpp里的train函数: // Train / Finetune a model. //训练,微调一个网络模型 int train() { // google的glog库,检查- ...

  10. vue 渲染流程

    1.DOM 节点树 高效的更新所有这些节点会是比较困难的,因为原生的DOM节点属性很多,渲染性能差. 2.虚拟 DOM “虚拟 DOM”是我们对由 Vue 组件树建立起来的整个 VNode 树的称呼. ...