SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)

REPEATS - Repeats

no tags 

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string

s = abaabaabaaba

is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.

Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string

u = babbabaabaabaabab

contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.

Input

In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.

Output

For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.

Example

Input:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b

Output:
4
since a (4, 3)-repeat is found starting at the 5th character of the input string.
ac代码

#include<stdio.h>         
#include<string.h>         
#include<algorithm>         
#include<iostream>        
#define min(a,b) (a>b?b:a)     
#define max(a,b) (a>b?a:b)      
using namespace std;        
char str[53030];      
int sa[53030],Rank[53030],rank2[53030],height[53030],c[53030],*x,*y,s[53030];    
int n;    
void cmp(int n,int sz)    
{    
    int i;    
    memset(c,0,sizeof(c));    
    for(i=0;i<n;i++)    
        c[x[y[i]]]++;    
    for(i=1;i<sz;i++)    
        c[i]+=c[i-1];    
    for(i=n-1;i>=0;i--)    
        sa[--c[x[y[i]]]]=y[i];    
}    
void build_sa(char *s,int n,int sz)    
{    
    x=Rank,y=rank2;    
    int i,j;    
    for(i=0;i<n;i++)    
        x[i]=s[i],y[i]=i;    
    cmp(n,sz);    
    int len;    
    for(len=1;len<n;len<<=1)    
    {    
        int yid=0;    
        for(i=n-len;i<n;i++)    
        {    
            y[yid++]=i;    
        }    
        for(i=0;i<n;i++)    
            if(sa[i]>=len)    
                y[yid++]=sa[i]-len;    
            cmp(n,sz);    
        swap(x,y);    
        x[sa[0]]=yid=0;    
        for(i=1;i<n;i++)    
        {    
            if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])    
                x[sa[i]]=yid;    
            else    
                x[sa[i]]=++yid;    
        }    
        sz=yid+1;    
        if(sz>=n)    
            break;    
    }    
    for(i=0;i<n;i++)    
        Rank[i]=x[i];    
}    
void getHeight(char *s,int n)    
{    
    int k=0;    
    for(int i=0;i<n;i++)    
    {    
        if(Rank[i]==0)    
            continue;    
        k=max(0,k-1);    
        int j=sa[Rank[i]-1];    
        while(s[i+k]==s[j+k])    
            k++;    
        height[Rank[i]]=k;    
    }    
}  
int minv[53010][20],lg[53030];    
void init_lg()  
{  
    int i;  
    lg[1]=0;  
    for(i=2;i<52020;i++)  
    {  
        lg[i]=lg[i>>1]+1;  
    }  
}  
void init_RMQ(int n)  
{  
    int i,j,k;  
    for(i=1;i<=n;i++)  
    {  
        minv[i][0]=height[i];  
    }  
    for(j=1;j<=lg[n];j++)    
    {    
        for(k=0;k+(1<<j)-1<=n;k++)    
        {    
            minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);     
        }    
    }  
}  
int lcp(int l,int r)  
{  
    l=Rank[l];  
    r=Rank[r];  
    if(l>r)  
        swap(l,r);  
    l++;  
    int k=lg[r-l+1];  
    return min(minv[l][k],minv[r-(1<<k)+1][k]);    
} 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int i,j,k;
		for(i=0;i<n;i++)
		{
			scanf("%s",str+i);
		}
		build_sa(str,n+1,128);
		getHeight(str,n);
		init_lg();
		init_RMQ(n);
		int maxn=0;
		for(i=1;i<n;i++)
		{
			for(j=0;j+i<n;j+=i)
			{
				int k=lcp(j,j+i);
				int now=k/i;
				int tj=j-(i-k%i);
				if(tj>=0)
				{
					if(lcp(tj,tj+i)>=i-k%i)
						now++;
				}
				if(now>maxn)
					maxn=now;
			}
		}
		printf("%d\n",maxn+1);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值