题目链接:https://vjudge.net/problem/POJ-2406

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 52631   Accepted: 21921

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

KMP:

求字符串的最小循环节。

1) 如果字符串长度能被“初步的最小循环节”整除,那么这个就是他的最小循环节。

2) 如果字符串长度不能被“初步的最小循环节”整除,那么这个只是它通过补全后的最小循环节。而它实际的最小循环节为自己。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
while(scanf("%s", x) && strcmp(x, "."))
{
int len = strlen(x);
get_next(x, len);
int r = len-Next[len]; //求出最小循环节
if(len%r) //如果长度/最小循环节除不尽,则对于此字符串来说,实际的最小循环节是自己
printf("1\n");
else
printf("%d\n", len/r);
}
}

后缀数组:

POJ2406 Power Strings —— KMP or 后缀数组 最小循环节的更多相关文章

  1. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  2. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

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

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

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

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

  5. Power Strings POJ - 2406 后缀数组

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  6. POJ2406 Power Strings(KMP)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56162   Accepted: 23370 Description Giv ...

  7. POJ2406 Power Strings KMP算法

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

  8. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  9. 【kmp算法】poj2406 Power Strings

    如果next[n]<n/2,一定无解. 否则,必须要满足n mod (n-next[n]) = 0 才行,此时,由于next数组的性质,0~n-next[n]-1的部分一定是最小循环节. [ab ...

随机推荐

  1. 初级Springboot(一)

    初级Springboot(一) 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 一.了解Springboot 做Java开发的小伙伴都知道,我们在做项目的时候,需要去写大量的配置文件 ...

  2. 跳转到指定页面popToViewController用法

    有人问popToViewController的用法 就写了下了 希望能帮到有需要的人 [self.navigationController popToViewController:[self.navi ...

  3. win7阻止iis开机启动

    https://zhidao.baidu.com/question/111234812.html 1.在"开始/运行/" 输入"services.msc" 启动 ...

  4. 3D投影

    3D投影方式的几大种类: 1.快门式 主动快门式即时分式,不过我们通常用前面的叫法,快门式3D眼镜(3D Shutter Glasses,也称作LC shutter glassesor active  ...

  5. C++精华笔记

    牛客微信推送的C++笔记:2016-12-12   14:23:26 1.C++不仅支持面向对象,也可以像C一样支持面向过程. 2.OOP三大特性:封装 继承 多态 3.函数重载依据:函数类型and形 ...

  6. Effective JavaScript Item 54 将undefined视为&quot;没有值&quot;

    将undefined视为"没有值" JavaScript中的undefined是一个特殊的值:当JavaScript没有提供详细的值时.它就会产生undefined. 比方: 未被 ...

  7. Spring集成JDBC

    不同spring版本合成的方式,有时候不一样,需要查看帮助文档来看如何集成,帮助文档在spring发行包中. 1.导入spring的包(这里吧Spring-3.1.3 Release的所有jar包都导 ...

  8. GG链路过多port不足导致的报错OGG-01223

    假设我们GG同步链路在增多.就有可能出现这个报错.在日志中能体现. 2014-05-20 13:32:38 WARNING OGG-01223 TCP/IP error 111 (Connection ...

  9. VS快速生成JSON数据类

    1.复制Json数据 2.

  10. openh264 在 osx 上的 nasm 问题

    先在 pc 上编译,熟悉一下. 编译遇到一个问题: nasm -DUNIX64 -DPREFIX -f macho64 -I./codec/common/x86/ -o codec/common/x8 ...