Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 45005   Accepted: 18792

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


只需要求n的错位部分n-f[n]是不是循环节
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+;
int n,f[N];
char p[N];
int main(){
int cas=;
while(true){
scanf("%s",p);
n=strlen(p);
if(p[]=='.') break;
f[]=f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&p[j]!=p[i]) j=f[j];
f[i+]=p[j]==p[i]?j+:;
}
if(n%(n-f[n])==) printf("%d\n",n/(n-f[n]));
else printf("1\n");
}
}

POJ2406Power Strings[KMP 失配函数]的更多相关文章

  1. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  2. POJ--2406Power Strings+KMP求字符串最小周期

    题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个 ...

  3. POJ2406-Power Strings(kmp循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31111   Accepted: 12982 D ...

  4. POJ1961[KMP 失配函数]

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 16776   Accepted: 8077 Descripti ...

  5. poj2406--Power Strings(kmp:求循环串的次数)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33163   Accepted: 13784 D ...

  6. HDU--1358--KMP算法失配函数getfail()的理解--Period

    /* Name: hdu--1358--Period Author: 日天大帝 Date: 20/04/17 10:24 Description: 长度/向后移动的位数 = 出现的次数 kmp其实匹配 ...

  7. POJ 2406Power Strings(KMP)

    POJ 2406 其实就是一个简单的kmp应用: ans = n % (n - f[n]) == 0 ? n / (n - f[n]) : 1 其中f是失配函数 //#pragma comment(l ...

  8. LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)

    题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个? 析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做 ...

  9. 转载 - kmp next函数 kmp的周期问题,深入了解kmp中next的原理

    出处:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html kmp next函数 kmp的周期问题,深入了解kmp中next的原理 ...

随机推荐

  1. 如何汉化XAF应用

    这是一个入门级的问题,应网友请求,总结一下XAF汉化过程的几个关键点. 一.所有Dev的控件的汉化,Dev官方有汉化文件.点击下载15.2版本. 正版用户登陆至官网是有专门的下载界面的,并且可以参与汉 ...

  2. Python中三目计算符的正确用法及短路逻辑

    今天在看别人代码时看到这样一种写法, 感觉是个挺容易踩到的坑, 搞清楚后写出来备忘. 短路逻辑 Python中进行逻辑运算的时候, 默认采用的是一种叫做短路逻辑的运算规则. 名字是很形象的, 下面直接 ...

  3. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  4. python批量下载图片的三种方法

    一是用微软提供的扩展库win32com来操作IE: win32com可以获得类似js里面的document对象,但貌似是只读的(文档都没找到). 二是用selenium的webdriver: sele ...

  5. Easticsearch通信方式_API

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch概念 a. Elasticsearch是一个基于Luc ...

  6. java语言中除数为零问题

    在以下几个例子中,输出结果如何? float aa=0; System.out.println(aa/0); System.out.println(1/aa); System.out.println( ...

  7. js中constructor和prototype

    在最开始学习js的时候,我们在讲到原型链和构造函数的时候经常会有一个例子 如果我们定义函数如下: function Foo() { /* .. */ } Foo.prototype.bar = fun ...

  8. transformjs:让天下没有难做的生意!不对,是特效!

    写在前面 transform是css3新增的一个属性,可是令开发者费解的是,其内部又有大量的属性如旋转.缩放.扭曲.平移,这也就导致了获取或者是设置transform中一个或者多个属性变得异常麻烦. ...

  9. JavaScript 变量声明提前

    <JavaScript权威指南>中指出:JavaScript变量在声明之前已经可用,JavaScript的这个特性被非正式的称为声明提前(hoisting),即JavaScript函数中声 ...

  10. Android 监听ScrollView的滑动

    我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部.可惜的是SDK并没有相应的方法,不过倒是提供了一个 protected void onScrollChanged ...