poj 2406 kmp求连续重复子串的个数
来源:互联网 发布:kinect for windows 编辑:程序博客网 时间:2024/06/07 11:18
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 33119 Accepted: 13775
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
abcdaaaaababab.
Sample Output
143
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01
#include<stdio.h>#include<string.h>#define N 1000005char str[N];int next[N];void getnext(char *str){ int i,n,j; n=strlen(str); next[0]=-1; j=-1; i=0; while(i<n) { if(j==-1||str[i]==str[j]) { i++; j++; next[i]=j; } else j=next[j]; }}int main(){ int n; while(scanf("%s",str)!=EOF) { if(strcmp(str,".")==0) break; n=strlen(str); getnext(str); if(n%(n-next[n])==0) printf("%d\n",n/(n-next[n])); else printf("1\n"); } return 0;}
用后缀数组写的超时啦。
/*做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足。判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为 O(n)。*/#include<stdio.h>#include<iostream>#include<string.h>using namespace std;#define N 1000005int t1[N],t2[N],x[N],c[N],sa[N],s[N],rank[N],height[N],b[N];char str[N];void build_sa(int *s,int n,int m){int *x=t1,*y=t2,i,k;for(i=0;i<m;i++) c[i]=0;for(i=0;i<n;i++) c[x[i]=s[i]]++;for(i=1;i<m;i++) c[i]+=c[i-1];for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;for(k=1;k<=n;k<<=1){int p=0;for(i=n-k;i<n;i++) y[p++]=i;for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;for(i=0;i<m;i++) c[i]=0;for(i=0;i<n;i++) c[x[y[i]]]++;for(i=1;i<m;i++) c[i]+=c[i-1];for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];swap(x,y);p=1; x[sa[0]]=0; for(i=1;i<n;i++)x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;if(p>=n)break;m=p;}}void getheight(int n){int i,k=0,j;for(i=0;i<=n;i++)rank[sa[i]]=i;for(i=0;i<n;i++){if(k) k--;j=sa[rank[i]-1];while(s[j+k]==s[i+k]) k++;height[rank[i]]=k;}}int main(){ int n,m,mi,i,k; while(scanf("%s",str)!=EOF) { n=strlen(str); for(i=0;str[i]!='\0';i++) s[i]=str[i]; s[n]=0; build_sa(s,n+1,200); getheight(n); //for(i=1;i<=n;i++) // printf("high=%d\n",height[i]); m=rank[0]; mi=height[m]; // printf("m=%d mi=%d\n",m,mi); for(i=m;i>1;i--) { //printf("%d %d\n",i,height[i]); if(height[i]<mi) mi=height[i]; // printf("mi=%d\n",mi); b[i-1]=mi; } mi=height[m+1]; for(i=m+1;i<=n;i++) { if(height[i]<mi) mi=height[i]; b[i]=mi; } for(k=1;k<=n;k++) { m=rank[k]; if(b[m]==n-k) break; } printf("%d\n",n/k); } return 0;}
0 0
- poj 2406 kmp求连续重复子串的个数
- poj 2406 Power Strings 【KMP求最小循环节】【后缀数组求连续重复子串】
- poj 3461 KMP算法求可重复利用的子串个数
- POJ 2406 后缀数组:求连续重复子串
- poj 2406 求连续重复子串出现的次数 后缀数组
- POJ 2406 Power Strings(KMP求一个串的重复子串)
- POJ-2406 Power Strings(KMP求重复子串出现的最大次数)
- POJ 2406 Power Strings 求连续重复字串(kmp)
- Maximum repetition substring+POj+后缀数组之求重复次数最多的连续重复子串
- POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)
- poj 2406 连续重复子串(后缀数组DC3/kmp)
- POJ 2406(连续重复子串)
- poj 2406 KMP 重复子串
- poj 3693 求出现次数最多的连续重复子串(具体的串)黑盒
- POJ 3693 Maximum repetition substring (求重复次数最多的连续子串,4级)
- POJ 3693 Maximum repetition substring (后缀数组+RMQ 求重复最多的连续子串)
- POJ 3693 Maximum repetition substring (后缀数组+RMQ 求重复最多的连续子串)
- hdu_1358 kmp 求连续子串
- 3.2主机探测与端口扫描
- XML学习笔记(一):XML基础及语法
- Unity发布Android安装文件
- 排序—希尔排序
- 3.3服务扫描与查点
- poj 2406 kmp求连续重复子串的个数
- Groovy闭包深入学习
- mysql默认引擎MyISAM引起的spring事务/session缓存失效
- 3.4网络漏洞扫描
- 超简单的麻将算法
- ML的BD框架-Hadoop.Mahout.Strom.Spark/GraphLab
- 迪克斯特拉算法(Dijkstra)
- 李一男离开华为时给属下的忠告
- android之HttpURLConnection