poj3693 Maximum repetition substring 后缀数组

http://poj.org/problem?id=3693

Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7241 Accepted: 2162

Description

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

Source


题意:求这个字符串内部循环重复次数最多的子串。输出字典序最小的。

思路:论文上的经典题。


就是先枚举长度L,表示长度为L的子串循环,肯定可以至少出现一次,出现两次以上枚举的时候肯定有s[i]==s[i+L],然后求这两个i和i+L后缀的最长公共前缀,记为k,那么至少出现了K/L+1,然后是是s[i]往前补够看能否多匹配一个。这样就得到了长度为L的子串最大循环次数。

对于求字典序最小的,我们枚举sa数组,判断sa[i],sa[i+L]后缀的最长公共前缀是否满足,遇到第一个满足就是字典序最小的。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int N=100005;
int sa[N]; //排第几的是哪个后缀
//sa[1~n]为有效值,sa[0]必定为n是无效值
int rank[N]; //rank后缀i排第几
//rank[0~n-1]为有效值,rank[n]必定为0无效值
int height[N]; //sa[i]和sa[i-1]的最长公共前缀
//height[2~n]为有效值
int t1[N],t2[N],c[N];
int dp[N][20];  //rmp数组
void build_sa(int s[],int n,int m)
{
    int *x=t1,*y=t2;
    //第一轮计数排序
    for(int i=0;i<m;i++) c[i]=0;
    for(int i=0;i<n;i++) c[x[i]=s[i]]++;
    for(int i=1;i<m;i++) c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    for(int j=1;j<=n;j<<=1)
    {
        int p=0;
        //直接利用sa数组排序第二关键字
        for(int i=n-j;i<n;i++) y[p++]=i;
        for(int i=0;i<n;i++)
            if(sa[i]>=j) y[p++]=sa[i]-j;
        //计数排序第一关键字
        for(int i=0;i<m;i++) c[i]=0;
        for(int i=0;i<n;i++) c[x[y[i]]]++;
        for(int i=1;i<m;i++) c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
        //根据sa和x数组计算新的x数组
        swap(x,y);
        p=1,x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
        if(p>=n) break;
        m=p;
    }
}
void getheight(int s[],int n)
{
    int k=0;
    for(int i=0;i<=n;i++)
        rank[sa[i]]=i;
    for(int i=0;i<n;i++)
    {
        if(k) k--;
        int j=sa[rank[i]-1];
        while(s[i+k]==s[j+k]) k++;
        height[rank[i]]=k;
    }
}
void initrmq(int n)
{
    for(int i=1;i<=n;i++) dp[i][0]=height[i];
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int lcp(int l,int r)
{
    int k=0;
    l=rank[l];
    r=rank[r];
    if(l>r) swap(l,r);
    l++;
    while((1<<(k+1))<=r-l+1) k++;
    return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
char s[N];
int a[N]; //待排序数组长度为n,放在0~n-1中,在最后面补一个0
int res[N];
int main()
{
    int cnt=0;
    while(~scanf("%s",s))
    {
        if(strcmp(s,"#")==0) break;
        int n=strlen(s);
        for(int i=0;i<n;i++)
            a[i]=s[i]-'a'+1;
        a[n]=0;
        build_sa(a,n+1,28);
        getheight(a,n);
        initrmq(n);
        int ans=0,tot=0;  //ans为最多可出现次数
        for(int l=1;l<=n/2;l++)    //枚举l长度子串可以匹配
        {
            for(int i=0;i+l<n;i+=l)
            {
                if(s[i]!=s[i+l]) continue;
                int k=lcp(i,i+l);
                int t=k/l+1;
                int r=i-(l-k%l);
                //printf("%d %d %d %d\n",i,i+l,k,k/l+1);
                if(r>=0&&lcp(r,r+l)>=k)  //往前看能否还能匹配一个
                    t++;
                if(t>ans)
                {
                    ans=t;
                    tot=0;
                    res[tot++]=l;
                }
                else if(ans==t)
                    res[tot++]=l;
            }
        }
        int len=0,ss;
        for(int i=1;i<=n&&!len;i++)  //枚举sa数组找字典序最小的
        {
            for(int j=0;j<tot;j++)
            {
                int l=res[j];
                if(lcp(sa[i],sa[i]+l)>=(ans-1)*l)
                {
                    len=l;
                    ss=sa[i];
                    break;
                }
            }
        }
        s[ss+len*ans]='\0';
        printf("Case %d: %s\n",++cnt,s+ss);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值