Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 29402   Accepted: 12296

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 两种做法:
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[];
int t;
int main()
{
char x;
while()
{
t=;
x=getchar();
if(x=='.')break;
while()
{
a[t++]=x;
if(x=='\n')break;
x=getchar();
}
int p=,k=;
t--;
for(int i=; i<t; i++)
{
if(a[i]==a[i%p])
k++;
else
{
if(p==k)k++;
p=k;
}
}
if(t%p==)
printf("%d\n",t/p);
else printf("1\n");
}
}
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[];
int t,next[];
void fun()
{
next[]=;
int i,j=;
for(i=;i<t;i++)
{
while(j>&&a[i]!=a[j])j=next[j-];
if(a[i]==a[j])j++;
next[i]=j;
}
}
int main()
{
char x;
while()
{
t=;
x=getchar();
if(x=='.')break;
while()
{
a[t++]=x;
if(x=='\n')break;
x=getchar();
}
t--;
fun();
if(t%(t-next[t-])==)
printf("%d\n",t/(t-next[t-]));
else printf("1\n");
}
}

Power Strings poj2406(神代码)的更多相关文章

  1. Power Strings[poj2406]题解

    Power Strings Description - Given two strings a and b we define ab to be their concatenation. For ex ...

  2. Power Strings POJ2406 KMP 求最小循环节

    相比一般KMP,构建next数组需要多循环一次,因为next[j]代表前j-1个字符的最长相同前缀后缀,比如字符串为aab aab aab共9个字符,则next[10]等于前9个字符中最长相同前缀后缀 ...

  3. 【POJ2406】 Power Strings (KMP)

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

  4. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

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

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

  6. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

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

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

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

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

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

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

随机推荐

  1. POI设置excel某列值为文本格式

    excel单元格格式默认为[常规],当某列限定必须从下拉框选择一个纯数字文本的时候,必须将单元格格式设置为[文本]. 否则即使输入的值和下拉框的一致,excel都认为输入的值是常规类型,而下拉框的值为 ...

  2. postman 第4节 切换环境和设置读取变量(转)

    postman提供了environment管理功能,想要在多个环境中测试,比如在测试环境.灰度环境.生产环境等,只需要用同样的接口,切换下环境即可,非常方便.具体步骤: 切换环境 1.点击界面右上角的 ...

  3. Thread类源码剖析

    目录 1.引子 2.JVM线程状态 3.Thread常用方法 4.拓展点 一.引子 说来也有些汗颜,搞了几年java,忽然发现竟然没拜读过java.lang.Thread类源码,这次特地拿出来晒一晒. ...

  4. C++栈和堆的生长方向

    C++内存区域分为5个区域.分别是堆,栈,自由存储区,全局/静态存储区和常量存储区. 栈:由编译器在需要的时候分配,在不需要的时候自动清除的变量存储区.里面通常是局部变量,函数参数等. 堆:由new分 ...

  5. C# checked和unchecked详解

    1.对基元类型执行的许多算术运算都可能造成溢出,有如下代码: Byte b=100; b=(Byte)(b+200); 简单的解读上面的代码: 第一步,将所有的操作数都扩大至32位或者64位(根据操作 ...

  6. 详解java设计模式之责任链模式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt175 从击鼓传花谈起 击鼓传花是一种热闹而又紧张的饮酒游戏.在酒宴上宾客依次 ...

  7. 利用Java调用OpenCV进行人脸识别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt409 今天我准备学习如何用Java来进行人脸检测.人脸检测有助于在任何数字图 ...

  8. Windows环境下最新OpenCV和Contribute代码的联合编译

    解决这个问题,目的在于获得并使用最新的完全版本的代码,主要方法是对CMake能够熟练使用,并且对编译等基础支持有所了解. 一.工具的准备 1 tortoisegit www.tortoisegit.o ...

  9. css入门基础知识

    一.CSS常用选择器 /*CSS注释*/ /*CSS修改页面中的所有标签必须借助选择器选中. 选择器中可以写多对CSS属性:每个属性名与属性值之间用:分隔,多对属性之间,必须用;分隔 选择器{ 属性1 ...

  10. 避免subList/subString陷阱

    避免subList/subString陷阱 java.util.List 接口提供了一个实例方法 List<E> subList(int fromIndex, int toIndex), ...