uva12338 - Anti-Rhyme Pairs 询问最长公共前缀 HASH+二分

Often two words that rhyme alsoend in the same sequence of characters. We use this property to define theconcept of an anti-rhyme. An anti-rhyme is a pair of words that have a similar beginning.The degree of anti-rhyme of a pair of words is further defined to be the lengthof the longest string S such thatboth strings start withS. Thus,“arboreal” and “arcturus” are an anti-rhyme pair of degree 2, while“chalkboard” and “overboard” arean anti-rhyme pair of degree 0.

 

You are given a list of words.Your task is, given a list of queries in the form(i, j), print the degree of anti-rhyme for the pair of stringsformed by thei-th and the j-th words from the list.

 

Input

Input consists of a number oftest cases. The first line of input contains the number of test casesT (T ≤ 35).Immediately following this line are Tcases.

 

Each case starts with the numberof strings N (1 ≤ N ≤105) on a line by itself. The followingN lines each contain a single non-emptystring made up entirely of lower case English characters ('a' to 'z'), whoselengthL is guaranteed to be lessthan or equal to 10,000. In everycase it is guaranteed thatN*L ≤106.

 

The line following the last stringcontains a single integer Q (1 ≤Q ≤ 106), the number of queries. Each of theQ lines following contain a querymade up of two integersi and j separated by whitespace (1 ≤ i, j ≤N).

 

Output

The outputconsists of T cases, each starting witha single line with“Case X:”, where X indicates the X-th case. There should be exactlyQ lines after that for each case. Each of those Q lines should contain an integer thatis the answer to the corresponding query in the input.

 

SampleInput                            Output for Sample Input

2

5

daffodilpacm

daffodiliupc

distancevector

distancefinder

distinctsubsequence

4

1 2

1 5

3 4

4 5

2

acm

icpc

2

1 2

2 2

Case 1:

8

1

8

4

Case 2:

0

4

  给出一些串,询问任意两个的公共前缀。

  对于每个串处理出所有前缀的hash值,询问的时候二分公共前缀长度,比较HASH值是否相等。

  二维数组开不下,可以开一维的记录位置。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;

const int MAXN=1000010;
const int MAXM=110;
const LL base=123;

unsigned long long hash[MAXN];
int T,N,Q,pos[MAXN],len[MAXN];
char str[MAXN];

int bsearch(int pos1,int pos2,int L,int R){
    while(L<R){
        int mid=L+(R-L+1)/2;
        if(hash[pos1+mid-1]==hash[pos2+mid-1]) L=mid;
        else R=mid-1;
    }
    return L;
}

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    scanf("%d",&T);
    while(T--){
        memset(hash,0,sizeof(hash));
        memset(len,0,sizeof(len));
        memset(pos,0,sizeof(pos));
        scanf("%d",&N);
        for(int i=1;i<=N;i++){
            scanf("%s",str+1);
            int L=strlen(str+1);
            len[i]=L;
            pos[i]=pos[i-1]+len[i-1];
            hash[pos[i]]=str[1]-'a'+1;
            for(int j=1;j<L;j++) hash[pos[i]+j]=hash[pos[i]+j-1]*base+str[j+1]-'a'+1;
        }
        printf("Case %d:\n",++cas);
        scanf("%d",&Q);
        while(Q--){
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d\n",bsearch(pos[a],pos[b],0,min(len[a],len[b])));
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值