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)的更多相关文章

  1. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  2. 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 ...

  3. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  4. 【poj3693】Maximum repetition substring(后缀数组+RMQ)

    题意:给定一个字符串,求重复次数最多的连续重复子串. 传说中的后缀数组神题,蒟蒻真的调了很久才对啊.感觉对后缀数组和RMQ的模版都不是很熟,导致还是会有很多各种各样的小错误= = 首先,枚举重复子串的 ...

  5. 2018.11.24 poj3693Maximum repetition substring(后缀数组)

    传送门 后缀数组好题. 考虑枚举循环节长度lenlenlen. 然后考虑枚举循环节的起点来更新答案. 但是直接枚举每次O(n)O(n)O(n). 考虑枚举len∗k+1len*k+1len∗k+1作为 ...

  6. SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解

    题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...

  7. HDU2459 后缀数组+RMQ

    题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...

  8. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  9. 【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 ...

随机推荐

  1. shell函数传递带空格的参数

    shell中的参数以空格为分割符,经常会碰到需要传递带空格的参数,例如传递带空格的文件名. 方法很简单:给参数加双引号. 但是实际效果要看你的函数内容,一种可能的情况是: 其实你真的传递进去了带空格的 ...

  2. ASP.NET MVC路径引用总结

    1.关于路径: (1)绝对路径 包含站点路径的路径:<a href=”http://www.baidu.com/about.jpg”>百度</a> 站点改变路径失效: (2)相 ...

  3. centOS中如何修改运行级别!

    在图形化界面可以用Ctrl+Alt+F2进入命令行窗口 * 假如你使用了虚拟机,有可能会出现不能进去的问题,原因是因为热键冲突 * 解决办法:修改热键就行了 edit→parameter→hot ke ...

  4. 【Python】Selenium元素定位错误之解决办法

    当使用class定位元素时发现报错: 错误信息:selenium.common.exceptions.InvalidSelectorException: Message: Compound class ...

  5. 基于Apache POI 向xlsx写入数据

    [0]写在前面 0.1) these codes are from 基于Apache POI 的向xlsx写入数据 0.2) this idea is from http://cwind.iteye. ...

  6. LeetCode222——Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  7. System.DateTime.Now.ToString()的一些用法

    日期处理函数    //2007年4月24日    this.TextBox6.Text = System.DateTime.Now.ToString("D");    //200 ...

  8. 消息队列Handler的用法

    下面是每隔一段时间就执行某个操作,直到关闭定时操作: final Handler handler = new Handler(); Runnable runnable = new Runnable() ...

  9. 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组

    [BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...

  10. linq to xml操作XML(转)

    转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...