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 ...
随机推荐
- 自己定义ViewGroup控件(二)----->流式布局进阶(二)
main.xml <?xml version="1.0" encoding="utf-8"? > <com.example.SimpleLay ...
- 软件工程第2次作业——Visual Studio 2017下基于C/C++的VSTS单元测试实践
Write one minute, test all day long. 环境确定 IDE:Microsoft Visual Studio 2017 Community 语言:C++ 单元测试工具:V ...
- substring,subsequence,charAt执行效率的不同
package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...
- 使用jquery改动表单的提交地址
基本思路: 通过使用jquery选择器得到相应表单的jquery对象,然后使用attr方法改动相应的action 演示样例程序一: 默认情况下,该表单会提交到page_one.html 点击butto ...
- 开源项目WebImageView载入图片
项目地址:https://github.com/ZaBlanc/WebImageView 作者对载入图片,以及图片的内存缓存和磁盘缓存做了封装. 代码量不多.可是可以满足一般的载入图片. 先看下项目结 ...
- 06 redis中set结构及命令详解
集合 set 相关命令 集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下 ...
- LeetCode(100)题解--Same Tree
https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...
- jQuery+bootstrap实现美化警告/确认/提示对话框插件
http://www.html580.com/12067/demo http://craftpip.github.io/jquery-confirm/
- TP框架---thinkphp模型
1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...
- GS与网络打交道
与网络打交道 在GS,GC,Share都与网络打交道,但还是GC最多 GC打交道过程 send_stat BaseChannel::SendCmdTry() { if (!m_queCmd.size( ...