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. [2014-02-19]如何移除响应头中的.net framework 版本信息 以及mvc版本信息?

    先来看一个简单mvc3网站的响应头 修改Global.asax文件 在Application_Start方法中添加如下代码 MvcHandler.DisableMvcResponseHeader = ...

  2. Ext.grid.EditorGridPanel点击单元格添加菜单栏

    1.定义菜单栏需要的全局变量 var khbm; var type; 2.新建一个菜单栏 var smenu = new Ext.menu.Menu({ id:"sMenu", i ...

  3. 使用nginx实现纯前端跨越

    你是否厌倦了老是依赖后台去处理跨域,把握不了主动权 你是否想模仿某个app倒腾一个demo,却困于接口无法跨域 那么很幸运,接下来我将现实不依赖任何后台,随心所欲的想访问哪个域名就访问哪个! 下载ng ...

  4. lucene&solr-day1

        全文检索课程 Lucene&Solr(1) 1.   计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a)   ...

  5. 红黑树的插入Java实现

    package practice; public class TestMain { public static void main(String[] args) { int[] ao = {5, 1, ...

  6. c# HttpWebRequest 模拟HTTP post 传递JSON参数

    //HTTP post   JSON 参数        private string HttpPost(string Url, Object ticket)        {            ...

  7. MPLS VPN随堂笔记3

    跨域 ASBR之间运行MPBGP 1.配置AS内部IGP保证环回口互相可达,同时建立LDP邻居 (优先启用 mpls label rang 16 100)方便查看实验现象 2.配置PE1-PE2 PE ...

  8. HTML canvas 笑脸

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 201521123004《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 答:主要以泛型为主 //简单的泛型类的定义,T为类型参数 public ...

  10. 201521123106 《Java程序设计》第6周学习总结

    1. 本章学习总结 2. 书面作业 Q1. clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 答:在同一个包里或者 ...