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. css常用文本属性

    [CSS常用文本属性] 1. 字体.字号类: ① font-weight: 字体粗细. bold-加粗.normal-正常.lighter-细体 也可以使用100-900数值,400表示normal, ...

  2. Web桌面应用框架2:著名的WEB桌面应用分析

    前一篇文章里,分析了包括NW.js和electron这种纯JS框架在内的几种Web桌面应用开发方式,实际上还有一种最古老的方式,那就是嵌入WebView的方式. 嵌入WebView的方式和整个程序都是 ...

  3. startsWith和endWith方法

    startsWith(): 例如:if(a.startsWith(b)) //判断字符串a 是不是以字符串b开头. 语法1 public boolean startsWith(String prefi ...

  4. NHibernate教程(21)——二级缓存(下)

    本节内容 引入 使用NHibernate二级缓存 启用缓存查询 管理NHibernate二级缓存 结语 引入 这篇我还继续上一篇的话题聊聊NHibernate二级缓存剩下的内容,比如你修改.删除数据时 ...

  5. 九度OJ 1010 A +B

    #include <iostream> #include <string> #include <sstream> using namespace std; int ...

  6. JAVA中String = null 与 String = "" 的区别

    JAVA中String = null 与 String = ""的区别 笔者今天在Debug的时候发现的NPE(NullPointerException),辛辛苦苦地调试了半天,终 ...

  7. 避免subList/subString陷阱

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

  8. 201521123069 《Java程序设计》 第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind List列表:元素以线性方式存放,允许有重复的对象. Set集:集合中的对象不按特定的方式排序,并且 ...

  9. 201521123007《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1. clone方法 1.1 Object ...

  10. for /r命令实现全盘搜索指定文件

    @echo off Rem :全盘搜索指定文件并输出到文本 set "fileName=Normal.dotm" set "outPutPath=C:\result.tx ...