POJ3693Maximum repetition substring (循环节)(后缀数组+RMQ)
The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1.
Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.
Input
The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.
The last test case is followed by a line containing a '#'.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.
Sample Input
ccabababc
daabbccaa
#
Sample Output
Case 1: ababab
Case 2: aa
题意:
求里面一段字符串,它的循环次数最多,如果有多个,输出最小字典序的一个。
思路就不说了,老题,可以去看论文。但是至少需要知道循环节的性质(具体的去KMP那里去看),大概是这样:
s1=x y z
s2= x y z。如果,s1=s2,且下面的x对应上面的y,下面的y对应上面的z,则x=y ,y=z,则x=y=z。那么他们(x,y,z)就是循环节,而且循环了4次,上面的xyz和下面的z。
下面说一下我的bug。。。
- 手写的swap居然WA了,写在头文件下面的东西找bug根本找不到啊。。。TT,找到的时候WA的一声就哭出来了。
//应该是因为疑惑运算超过int了
int min(int a,int b) { if(a<b) return a;return b;}
void swap(int a,int b) {a^=b;b^=a;a^=b;}
- 逻辑运算少加了括号。
//注意下面的括号,丢了很严重,不要问我怎么知道的
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;
const int maxn=;
char ch[maxn];
int min(int a,int b) { if(a<b) return a;return b;}
struct SA
{
int Rank[maxn],sa[maxn],tsa[maxn],A[maxn],B[maxn],cntA[maxn],cntB[maxn],N;
int ht[maxn],Min[maxn][];
int sort()
{
N=strlen(ch+);
for(int i=;i<=;i++) cntA[i]=;
for(int i=;i<=N;i++) cntA[ch[i]-'a'+]++;
for(int i=;i<=;i++) cntA[i]+=cntA[i-];
for(int i=N;i>=;i--) sa[cntA[ch[i]-'a'+]--]=i;
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(ch[sa[i]]==ch[sa[i-]]?:);//这个括号不要少。
for(int l=;Rank[sa[N]]<N;l<<=){
for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
Rank[sa[]]=;//注意下面的括号,丢了很严重,不要问我怎么知道的
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
}
}
int height(){
for(int i=,j=;i<=N;i++){
if(j) j--;
while(ch[i+j]==ch[sa[Rank[i]-]+j]) j++;
ht[Rank[i]]=j;
}
}
int get_rmq()
{
for(int i=;i<=N;i++) Min[i][]=ht[i];
for(int j=;(<<j)<=N;j++){
for(int i=;i+(<<j)-<=N;i++)
Min[i][j]=min(Min[i][j-],Min[i+(<<(j-))][j-]);
}
}
int query_rmq(int L,int R)
{
if(L>R) swap(L,R); L++;
int k=log2(R-L+);
return min(Min[L][k],Min[R-(<<k)+][k]);
}
void solve()//solve这一段是抄的,说实话我觉得有点乱。。。
{
int ans=,al=,ar=;
for(int L=;L*<=N;L++){
for(int i=;L*(i+)+<=N;i++){
int x=L*i+,y=L*(i+)+;
if(ch[x]!=ch[y]) continue;
int z=query_rmq(Rank[x],Rank[y]);
int t1=y+z-,t0=;
for(int j=;j<=L-;j++){
if(x-j< || ch[x-j]!=ch[y-j]) break;
t0=x-j;int now=((t1-t0+)/L);
if(now>ans || (now==ans && Rank[t0]<Rank[al])){
ans=now;al=t0;ar=t0+now*L-;
}
}
}
}
if(ans==) printf("%c\n",ch[sa[]]);
else {
for(int i=al;i<=ar;i++) printf("%c",ch[i]);printf("\n");
}
}
}Sa;
int main()
{
int Case=;
while(~scanf("%s",ch+)){
if(ch[]=='#') return ;
printf("Case %d: ",++Case);
Sa.sort();
Sa.height();;
Sa.get_rmq();
Sa.solve();
} return ;
}
POJ3693Maximum repetition substring (循环节)(后缀数组+RMQ)的更多相关文章
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- spoj687 REPEATS - Repeats (后缀数组+rmq)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- 【poj3693】Maximum repetition substring(后缀数组+RMQ)
题意:给定一个字符串,求重复次数最多的连续重复子串. 传说中的后缀数组神题,蒟蒻真的调了很久才对啊.感觉对后缀数组和RMQ的模版都不是很熟,导致还是会有很多各种各样的小错误= = 首先,枚举重复子串的 ...
- 2018.11.24 poj3693Maximum repetition substring(后缀数组)
传送门 后缀数组好题. 考虑枚举循环节长度lenlenlen. 然后考虑枚举循环节的起点来更新答案. 但是直接枚举每次O(n)O(n)O(n). 考虑枚举len∗k+1len*k+1len∗k+1作为 ...
- SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解
题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...
- HDU2459 后缀数组+RMQ
题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)
The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...
随机推荐
- python 中给文件加锁——fcntl模块
如果没有fcntl模块则用 sudo pip install fcntl安装 模块简单说明: 打开文件,不存在则创建之 f = open('./test','w') fcntl.flock(f,fcn ...
- robotframework安装appium
安装: Appium-Python-Client,在运行的cmd下输入:pip install Appium-python-Client 安装:robotframework-appiumlibrary ...
- USB协议[转]__总结得很好
一 枚举过程:◆ 用户将一个USB设备插入USB端口,主机为端口供电,设备此时处于上电状态.◆主机检测设备.◆集线器使用中断通道将事件报告给主机.◆主机发送Get_Port_Status(读端口状态) ...
- MySQL数据表导出某条记录
请按照步骤导出,否则可能会报错: ERROR (HY000): The MySQL server is running with the --secure-file-priv option so it ...
- items" does not support runtime expression
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为 <%@tagl ...
- java 面试总结
1.static变量与实体变量的差别? static是静态变量,static能够通过类名直接訪问 内存方面的不同:static在定义的时候jvm就会分配空间, 而实体变量仅仅有在创建对象的时候才会去分 ...
- python从安装与使用pip到入门
官方下载地址:https://www.python.org/downloads/ 下载后直接安装就可以了 再配一下环境变量, cmd运行python -V (注意,这里是大写的V) 打开python跑 ...
- 设计TCP服务器的规则
设计TCP服务器,采用如下规则: 1.正等待连接请求的一端有一个固定长度的连接队列,该队列中的连接已被TCP接受(完成三次握手),但还没有被应用层接受.注意:TCP接受一个连接是将其放入这个队列,而应 ...
- 计算机网络--DNS
1.域名系统DNS(domain name system)是因特网使用的命名系统,用来把便于人们时用的机器名字转换为IP地址.因特网的域名系统DNS被设计成一个联机分布式数据库系统,并采用客户服务器方 ...
- SAM4E单片机之旅——13、LCD之ASF初步
在Atmel Studio 6中,集成了Atmel Software Framework(ASF框架).通过它提供的库,可以很快速地完成新的项目. 这次的最终目标使用ASF在LCD上显示出文字“Hel ...