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

分析: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. android_orm框架之greenDAO(一)

    目录: 一.概述 二.下载并解压greenDAO相关资源 三.应用greenDAO框架 1.创建Java工程 2.添加类库支持 3.创建类 四.代码分析 五.使用greenDAO 六.源码下载 一.概 ...

  2. Redis自学笔记—PHP

    connect 实例连接到一个Redis. $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); var_dum ...

  3. php中120个内置函数

    php中实现事件模式 https://yq.aliyun.com/ziliao/162660 <?php class Event{ private $events = []; public fu ...

  4. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  5. CAD打开慢,卡在99%

    问题描述 打开AutoCAD的时候,软件停留在加载99%,点击出现[无法响应],要么等待,要么强行关闭,若平时正常关闭CAD时也异常缓慢. 原因分析 破解版,没有联网就激活了.CAD默认启动需要联网, ...

  6. Linux文件类型(学习笔记六)

    一.Linux下的文件类型 普通文件:在由 ls –al 所显示出来的属性方面,第一个属性为 [ - ] 目录文件:在由 ls –al 所显示出来的属性方面,第一个属性为 [ d ] 设备文件:一般都 ...

  7. 【转】Spring中IoC的优点与缺点

    1. 优点 我们知道,在Java基本教程中有一个定律告诉我们:所有的对象都必须创建:或者说:使用对象之前必须创建,但是现在我们可以不必一定遵循这个定律了,我们可以从Ioc容器中直接获得一个对象然后直接 ...

  8. PyQt5——安装Eric6

    Eric6是PyQt编程最理想的IDE.windows版的安装很简单.下面的安装也是在windows上进行的.linux版的我安装有点问题,有时间再折腾. 下载: Eric6官网:http://eri ...

  9. Docker网络一览

    转自:http://dockone.io/article/1143 [编者的话]本文是Nuage Networks公司Filip Verloy的一篇博文,简介了一下Docker网络情况,单主机的四种模 ...

  10. html5调用摄像头实现拍照

    技术时刻都在前进着.我们的需求也是时刻在改变着.最近在开发中遇到了用户进行账号注册时需要个人图像,网站提供自动拍照功能.还有在登录了PC之后,手机端进行登录时只需要扫描一下PC上的二维码就可以登录.这 ...