Power Strings

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

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.
 
 
【题意】
  一个字符串的最大重复字串。
  即一个字符串是一个子串最多重复多少次得到的。
 
【分析】
  

  ① len==1||s[next[len-1]]!=s[len-1] ans=1;
  ② len%(len-next[len])==0 ans= len/(len-next[len]);

  原因如图:

  

代码如下:

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. #define Maxn 1000010
  9.  
  10. char s[Maxn];
  11. int len,nt[Maxn];
  12.  
  13. void kmp()
  14. {
  15. nt[]=;
  16. int p=;
  17. for(int i=;i<=len;i++)
  18. {
  19. while(s[i]!=s[p+]&&p) p=nt[p];
  20. if(s[i]==s[p+]) p++;
  21. nt[i]=p;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. int n,i;
  28. while()
  29. {
  30. gets(s+);
  31. len=strlen(s+);
  32. if(len==&&s[]=='.') break;
  33. kmp();
  34. if(len%(len-nt[len])==) printf("%d\n",len/(len-nt[len]));
  35. else printf("1\n");
  36. }
  37. }

[POJ2406]

2016-07-20 16:30:02

【POJ2406】 Power Strings (KMP)的更多相关文章

  1. 【POJ2406】Power Strings(KMP,后缀数组)

    题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longi ...

  2. poj2406 Power Strings(kmp)

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

  3. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  4. Power Strings(KMP)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 D ...

  5. 【TOJ 2406】Power Strings(KMP找最多重复子串)

    描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc& ...

  6. 【POJ 2406】Power Strings(KMP循环节)

    终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...

  7. jzoj3208. 【JSOI2013】编程作业(kmp)

    题面 Description Will相信,很多同学都有过这样的经历:大牛已经写好了编程作业,而作为菜鸟的自己不会写怎么办呢?拿大牛的代码抄一下嘛!但是提交一模一样的作业是不是不太好?于是就改一改变量 ...

  8. 【CH1809】匹配统计(KMP)

    题目链接 摘自https://www.cnblogs.com/wyboooo/p/9829517.html 用KMP先求出以a[i]为结尾的前缀与b匹配的最长长度. 比如 f[i] = j,就表示a[ ...

  9. 【poj2406】 Power Strings

    http://poj.org/problem?id=2406 (题目链接) 题意 给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值. Solution 后 ...

随机推荐

  1. cobbler部署

    1.cobbler介绍 Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows.该工具使用python开发,小巧轻便(才15k行python代码),使用简单的 ...

  2. ruby gem源更换国内源gems.ruby-china.org数据源

    gem sources -l gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ 更新缓存 ge ...

  3. Python之路【第二十一篇】:Django之Form组件

    Django之Form组件   Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试牛刀 1. ...

  4. Eval()和DataBinder Eval(Container DataItem,)的区别及用法

        ASP.NET 2.0改善了模板中的数据绑定操作把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fiel ...

  5. mysql更改root密码及root远程登录

    1.更改root密码 use mysql; update user set password=password('petecc') where user='root'; 2.root远程登录 1 up ...

  6. iOS开发——音频篇——音效的播放

    一.简单介绍 简单来说,音频可以分为2种 (1)音效 又称“短音频”,通常在程序中的播放时长为1~2秒 在应用程序中起到点缀效果,提升整体用户体验 (2)音乐 比如游戏中的“背景音乐”,一般播放时间较 ...

  7. 0基础学习ios开发笔记第二天

    C语言的基本结构 c语言的入口函数是main函数. main函数的返回值行业标准是int return 数字:返回值 每条语句最后以分号结尾 注释:行注释.块注释 int main(void) { / ...

  8. 锋利的Jquery解惑系列(二)------插件开发大总结

    申明:插件开发是实际项目就经常用到的,不过也是挺吃力的.笔者自己做项目时,看着我们老大写的jQuery一头桨糊,那叫个痛苦.后面果断买了本参考书以及浏览别人的博客,现在也算慢慢入门了.现在总结自己的一 ...

  9. spark - 从HDFS加载文件并分析

    scala> val file=sc.textFile("/workspace/bpUserinfo_logs/bpUserinfo_20160212.log") scala ...

  10. 【原创】Android多个xml文件的使用

    Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我 ...