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

  1. abcd
  2. aaaa
  3. ababab
  4. .

Sample Output

  1. 1
  2. 4
  3. 3
  4.  
  5. 两种做法:
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. char a[];
  6. int t;
  7. int main()
  8. {
  9. char x;
  10. while()
  11. {
  12. t=;
  13. x=getchar();
  14. if(x=='.')break;
  15. while()
  16. {
  17. a[t++]=x;
  18. if(x=='\n')break;
  19. x=getchar();
  20. }
  21. int p=,k=;
  22. t--;
  23. for(int i=; i<t; i++)
  24. {
  25. if(a[i]==a[i%p])
  26. k++;
  27. else
  28. {
  29. if(p==k)k++;
  30. p=k;
  31. }
  32. }
  33. if(t%p==)
  34. printf("%d\n",t/p);
  35. else printf("1\n");
  36. }
  37. }
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. char a[];
  6. int t,next[];
  7. void fun()
  8. {
  9. next[]=;
  10. int i,j=;
  11. for(i=;i<t;i++)
  12. {
  13. while(j>&&a[i]!=a[j])j=next[j-];
  14. if(a[i]==a[j])j++;
  15. next[i]=j;
  16. }
  17. }
  18. int main()
  19. {
  20. char x;
  21. while()
  22. {
  23. t=;
  24. x=getchar();
  25. if(x=='.')break;
  26. while()
  27. {
  28. a[t++]=x;
  29. if(x=='\n')break;
  30. x=getchar();
  31. }
  32. t--;
  33. fun();
  34. if(t%(t-next[t-])==)
  35. printf("%d\n",t/(t-next[t-]));
  36. else printf("1\n");
  37. }
  38. }

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]ConfigurationSection:让web.config配置更有条理

    本文针对新手 使用Web.config的配置信息,一般都习惯于使用 ConfigurationManager.AppSettings["ConfigKey"] 当程序不断迭代,开发 ...

  2. live事件的替代方法on的使用注意事项

    根据jQuery的官方描述,live方法在1.7中已经不建议使用,在1.9中删除了这个方法.并建议在以后的代码中使用on方法来替代. on方法可以接受三个参数:事件名.触发选择器.事件函数. 需要特别 ...

  3. .net分布式压力测试工具(Beetle.DT)

    肯定有人会问为什么会写这样一个开源工具?和现有的有什么差别?不过对于一个程序员来说写东西还真不需要理由的:),主要原因是工作有点闲(开玩笑),不过说实话一个程员怎可能会停止写代码呢(作为一个奔4的程序 ...

  4. JSP页面中<%!%>与<%%>与<%=%>

    首先,我们要了解jsp运行原理.JSP的本质就是一个Servlet,JSP的运行之前会先被Tomcat服务器翻译为.java文件,然后在将.java文本编译 为.class文件,而我们在访问jsp时, ...

  5. 递归的二叉查找树Java实现

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

  6. LVS之DR模式实战及高可用性

    author:JevonWei 版权声明:原创作品 LVS-DR实现同网段调度web模式 拓扑环境 网络环境 RS1 RIP 192.168.198.138/24 VIP 192.168.198.10 ...

  7. Centos 6修复/boot目录及fstab等系统文件

    author:JevonWei 版权声明:原创作品 错误界面 系统修复过程中,若需要修复fatab挂载文件,磁盘分区为lvm逻辑卷格式,则默认在修复模式下处于不可活动状态,需使用vgchage -ay ...

  8. 【C#多线程编程实战笔记】二、 线程同步

    使用Mutex类-互斥锁 owned为true,互斥锁的初始状态就是被主线程所获取,否则处于未获取状态 name为定义的互斥锁名称,在整个操作系统只有一个命名未CSharpThreadingCookb ...

  9. Project 7:自然数的拆分

    自然数的拆分:任何一个大于1的自然数N,总可以拆分成若干个自然数之和,并且有多种拆分方法.例如自然数5,可以有如下一些拆分方法: 5=1+1+1+1+1 5=1+1+1+2 5=1+2+2 5=1+4 ...

  10. JavaScript二维码生成——qrcode.js

    在开发中,有时候,我们需要根据不同的内容来动态生成二维码,则可以使用qrcode.js这个小插件来实现. 1.qrcode.js文件内容: (1)未压缩(qrcode.js): /** * @file ...