题目链接:https://vjudge.net/problem/POJ-2406

kmp学习:https://blog.csdn.net/starstar1992/article/details/54913261/

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.
 
题目大意:问你一个子串的最多多少次方能构成完整的串
思路:其实这题本人是没有想到next数组能直接求出来的 ,后来看了一下题解,大概意思如下:
比如:  ababab,显然最后一位的next数组是3(这里以0开始),所以0~3位和2~5位是一样的,所以0~1和2~3是一样的,2~3和4~5是一样的,所以最多次方数就是3个
比如:ababa,显然最后一位的next数组是3,所以0~2位和2~4位是一样的,所以0~1和2~3是一样的,但是还剩下一位,所以最大只能是1  
综上所述,其实就是求最后一位的next数组,然后总长减去它,看总长是否能够整除这个值,能的话直接输出整除的值,不能的话,答案就是1
看一下证明:

-----------------------------

-----------------------------

k    m        x      j       i

由上,next【i】=j,两段红色的字符串相等(两个字符串完全相等),s[k....j]==s[m....i]

设s[x...j]=s[j....i](xj=ji)

则可得,以下简写字符串表达方式

kj=kx+xj;

mi=mj+ji;

因为xj=ji,所以kx=mj,如下图所示

-------------

-------------

k   m        x     j

看到了没,此时又重复上面的模型了,kx=mj,所以可以一直这样递推下去

所以可以推出一个重要的性质len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]);

证明来源:https://blog.csdn.net/hp_satan/article/details/18085919

看代码:
  1. #include<iostream>
  2. #include<string.h>
  3. #include<map>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<stdio.h>
  7. #include<cmath>
  8. #include<ctype.h>
  9. #include<math.h>
  10. #include<algorithm>
  11. #include<set>
  12. #include<queue>
  13. typedef long long ll;
  14. using namespace std;
  15. const ll mod=;
  16. const int maxn=1e8+;
  17. const int maxk=5e3+;
  18. const int maxx=1e4+;
  19. const ll maxe=+;
  20. #define INF 0x3f3f3f3f3f3f
  21. #define Lson l,mid,rt<<1
  22. #define Rson mid+1,r,rt<<1|1
  23. char a[maxn];
  24. int next[maxn];
  25. void cal_next()
  26. {
  27. next[]=-;
  28. int k=-;
  29. int len=strlen(a);
  30. for(int i=;i<len;i++)
  31. {
  32. while(k>-&&a[k+]!=a[i])
  33. {
  34. k=next[k];
  35. }
  36. if(a[k+]==a[i]) k++;
  37. next[i]=k;
  38. }
  39. }
  40. int main()
  41. {
  42. //while(cin>>a)
  43. while(scanf("%s",a)!=EOF)
  44. {
  45. if(a[]=='.') break;
  46. cal_next();
  47. int len=strlen(a);
  48. if(len%(len-next[len-]-)==) printf("%d\n",len/(len-next[len-]-));
  49. else printf("1\n");
  50. }
  51. return ;
  52. }
 

poj(2406) kmp的更多相关文章

  1. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  2. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

  3. POJ 2406 KMP next数组的应用

    题意:让你找最小重复串的个数 加深KMP中对next数组的理解 #include <cstdio> #include <cstring> using namespace std ...

  4. POJ 2406 KMP 循环节

    给一个字符串.求这个串的最小的循环节的长度. 好像.num = len/(len-next[len]) 就是循环节的长度.如果 len%(len-next[len]) ==0 就是 说字符串长度刚好是 ...

  5. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  6. POJ 2406 Power Strings

    F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  7. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

  8. Power Strings POJ - 2406

    Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...

  9. POJ - 2406 ~SPOJ - REPEATS~POJ - 3693 后缀数组求解重复字串问题

    POJ - 2406 题意: 给出一个字符串,要把它写成(x)n的形式,问n的最大值. 这题是求整个串的重复次数,不是重复最多次数的字串 这题很容易想到用KMP求最小循环节就没了,但是后缀数组也能写 ...

随机推荐

  1. 一个Web结合Mybatis项目

    需要引入apache.commons.dbcp-1.2.2.osgi.jar以及org.apache.commons.pool-1.5.3.jar用来提供JDBC的访问: 需要org.springfr ...

  2. windows拾遗

    Files has invalid value "<<<<<<< .mine". Illegal characters in path.在 ...

  3. nginx利用proxy_cache来缓存文件

    为什么要做web cache,我想大家最主要的是解决流量的压力.随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力.与此同时某些网站的页面 ...

  4. TModJS:template

    ylbtech-TModJS: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech. ...

  5. [快手(AAuto)学习笔记]如何让程序在运行时请求管理员权限(UAC)

    作者:ffsystem 作为(糟糕的)程序猿,习惯写代码解决一些简单事务.正常用批处理就能解决大部分工作,复杂一点用AutoIt 3. 有时候要分发给别人,就需要一个界面.外行你程序写得如何他看不懂, ...

  6. java电子书chm全套下载

    链接:http://pan.baidu.com/s/1qWmMlYk 密码:us3x 版权声明:本文为博主原创文章,未经博主允许不得转载.

  7. 【总结整理】JQuery基础学习---动画

    jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...

  8. Flask02 路由的书写、蓝图、利用蓝图实现url前缀、利用蓝图实现子域名、访问静态文件

    1 书写路由的两种方法 1.1 利用Flask实例对象的 add_url_rule 方法实现 该方法有一个必填参数,两个默认参数 · rule : 请求路径的规则 endpoint : 端点,默认值是 ...

  9. 关于login/interactive/no-interactive shell和profile/bash_profile/bashrc

    login shell:第一次登录进系统时的shell,一般是指本机启动时的控制台shell或者ssh远程登录时的shell. interactive shell:登录以后,再打开控制台时运行的she ...

  10. R: factor & list 生成和操作因子、列表

    ################################################### 问题:生成.操作列表 & 因子   18.4.27 怎么生成列表 list.因子 fac ...