B. Power Strings

Time Limit: 3000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 
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.
 
解题:求字符串的循环节长度。利用KMP的适配数组。如果字符长度可以被(字符长度-fail[字符长度])整除,循环节这是这个商,否则循环节长度为1,即就是这个字符本身。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
char str[maxn];
int fail[maxn];
void getFail(int &len) {
int i,j;
len = strlen(str);
fail[] = fail[];
for(i = ; i < len; i++) {
j = fail[i];
while(j && str[j] != str[i]) j = fail[j];
fail[i+] = str[j] == str[i] ? j+:;
}
}
int main() {
int len;
while(gets(str) && str[] != '.') {
getFail(len);
if(len%(len-fail[len])) puts("");
else printf("%d\n",len/(len-fail[len]));
}
return ;
}

xtu字符串 B. Power Strings的更多相关文章

  1. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

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

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

  3. POJ 2406 Power Strings (KMP)

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

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

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

  5. POJ 2406:Power Strings

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

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

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

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

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

  8. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  9. Power Strings

    Power Strings TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1791   Accepted: 528 Descr ...

随机推荐

  1. jQuery将json字符串显示在页面上

    js代码: function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, un ...

  2. [BZOJ3527][ZJOI2014]力 FFT+数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 首先卷积的形式是$h(i)=\sum_{i=0}^jf(i)g(i-j)$,如果我们 ...

  3. 列表、margin和padding的探讨、标签的分类

    一.列表 列表分为无序列表.有序列表和自定义列表 1.无序列表   <ul></ul> 1).内部必须有子标签,<li></li> 2).ul天生自带内 ...

  4. Git-merge & rebase(变基)

    在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. merge: merge有两种方法,fast-forward(快速合并)和three-way merge(三方合并 ...

  5. casting in C++

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39 February 20, 2013 casting in C++ Fil ...

  6. JDBC的fetchsize和maxrows

    在我们的项目开发中,可能有把SQL查询的结果保存到CSV然后提供下载的功能.当查询的结果集相当大的时候,很容易报内存不足错误(outofmemory).那该怎么解决这种情况的内存不足错误呢? 其实在J ...

  7. innerHTML引起IE的内存泄漏

      内存泄漏常见的原因有三种: 1. 闭包 2. 未解除事件绑定 3. 循环引用DOM元素 除此之外,还有一种泄漏原因少有人知,它和innerHTML有关,不过很容易解决. 出现这种内存泄漏需要有三个 ...

  8. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  9. strict说明

  10. 主成分分析、因子分析、ICA(未完成)

    并且SVD分解也适用于一般的矩阵. 主成分分析可以简单的总结成一句话:数据的压缩和解释.常被用来寻找判断某种事物或现象的综合指标,并且给综合指标所包含的信息以适当的解释.在实际的应用过程中,主成分分析 ...