UVA - 12338 Anti-Rhyme Pairs (suffix array)

Topic links: Click here

Title effect: given n and m query string, the query given every two string index, length of the longest common prefix ask these two strings

Topic Analysis: Because of the need to ask longest common prefix, and m to the query to 1e6, it also requires us to each inquiry must be completed within the time complexity of (1) or O (logn) of O, so we can choose the suffix array the n string stitched together, the array after ran height, with about RMQ pretreatment, can be O (1) obtaining two longest common prefix of the string

Remember to pay attention to the size of the array, because we need to add up to N number of special characters, and the total length of the original strings have 1e6, so the length of the suffix array should be 1e6 + 1e5, then st table, then opened the second dimension should be at least 21, because 2 is less than 20 1e6 + 1e5 power of

Code:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;

typedef long long LL;

const int inf=0x3f3f3f3f;

const int N=2e6+100;

char str[N];

int sa[N]; //SA数组,表示将S的n个后缀从小到大排序后把排好序的
//的后缀的开头位置顺次放入SA中
int t1[N],t2[N],c[N];

int rk[N],height[N],id[N],str_len[N],len;

int s[N];

int st[N][25];
 
void build_sa(int s[],int n,int m)//n为添加0后的总长
{
    int i,j,p,*x=t1,*y=t2;
    for(i=0;i<m;i++) 
		c[i]=0;
    for(i=0;i<n;i++) 
		c[x[i]=s[i]]++;
    for(i=1;i<m;i++) 
		c[i]+=c[i-1];
    for(i=n-1;i>=0;i--) 
		sa[--c[x[i]]]=i;
    for(j=1;j<=n;j<<=1) 
	{
        p=0;
        for(i=n-j;i<n;i++) 
			y[p++]=i;
        for(i=0;i<n;i++) 
            if(sa[i]>=j) 
                y[p++]=sa[i]-j;
        for(i=0;i<m;i++) 
			c[i]=0;
        for(i=0;i<n;i++) 
			c[x[y[i]]]++;
        for(i=1;i<m;i++) 
			c[i]+=c[i-1];
        for(i=n-1;i>=0;i--) 
			sa[--c[x[y[i]]]]=y[i];
        swap(x,y);
        p=1,x[sa[0]]=0;
        for(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 get_height(int s[],int n)//n为添加0后的总长
{
    int i,j,k=0;
    for(i=0;i<=n;i++)
        rk[sa[i]]=i;
    for(i=0;i<n;i++) 
	{
        if(k) 
			k--;
        j=sa[rk[i]-1];
        while(s[i+k]==s[j+k]) 
			k++;
        height[rk[i]]=k;
    }
}

void solve(int base=128)
{
	build_sa(s,len+1,base);
	get_height(s,len);
}

void ST_build()
{
	for(int i=1;i<=len;i++)
		st[i][0]=height[i];
	for(int i=1;i<=log2(len);i++)
		for(int j=1;j+(1<<i)-1<=len;j++)
			st[j][i]=min(st[j][i-1],st[j+(1<<i-1)][i-1]);
}

int ST_query(int a,int b)
{
	int l=rk[a],r=rk[b];
	if(l>r)
		swap(l,r);
	l++;
	int k=log2(r-l+1);
	return min(st[l][k],st[r-(1<<k)+1][k]);
}

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	int kase=0;
	while(w--)
	{
		int n;
		scanf("%d",&n);
		int pos=30;
		len=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%s",str);
			id[i]=len;
			str_len[i]=strlen(str);
			for(int j=0;str[j];j++)
				s[len++]=str[j]-'a'+1;
			s[len++]=pos++;
		}
		s[len]=0;
		solve(pos);
		ST_build();
		int m;
		scanf("%d",&m);
		printf("Case %d:\n",++kase);
		while(m--)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			if(l==r)
				printf("%d\n",str_len[l]);
			else
				printf("%d\n",ST_query(id[l],id[r]));
		}
	}



	
	
	
	
	
	
	
	
	
	return 0;
}

 

Published 558 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_45458915/article/details/104039703