UVA 12338 Anti-Rhyme Pairs (字符串哈希+二分)

Anti-Rhyme Pairs

Time Limit: 5000ms
Memory Limit: 131072KB
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 with S. Thus,arboreal and arcturus are an anti-rhyme pair of degree 2, whilechalkboard 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 the i-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 cases T (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 following N lines each contain a single non-emptystring made up entirely of lower case English characters ('a' to 'z'), whoselength L is guaranteed to be lessthan or equal to 10,000. In everycase it is guaranteed that N*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 integers i 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 exactly Q 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

Warning:I/O files is huge, make sure your I/O is fast.

 

题目链接:https://acm.bnu.edu.cn/v3/contest_show.php?cid=7985#problem/D

题目大意:给n个字符串,m个查询,求第x个和第y个的最长公共前缀长度

题目分析:对字符串按前缀hash,二分长度判断即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int const MOD = 1e9 + 7;
char s[MAX];
int n, m, st, ed, le[MAX];
vector<int> vt[MAX];

void get_hash(int id)
{
    vt[id].clear();
    ll tmp = 0;
    for(int j = 0; j < le[id]; j++)
    {
        tmp = (tmp * 101ll + (ll)s[j]) % MOD;
        vt[id].push_back(tmp);
    }
}

bool judge(int x)
{
    if(x == 0)
        return true;
    if(vt[st][x - 1] != vt[ed][x - 1]) 
        return false;
    return true;
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        printf("Case %d:\n", ca);
        scanf("%d", &n);
        getchar();
        for(int i = 0; i < n; i++)
        {
            gets(s);
            le[i] = (int) strlen(s);
            get_hash(i);
        }
        scanf("%d", &m);
        while(m --)
        {
            scanf("%d %d", &st, &ed);
            st --;
            ed --;
            int l = 0, r = min(le[st], le[ed]), mid, res = 0;
            while(l <= r)
            {
                mid = (l + r) >> 1;
                if(judge(mid))
                {
                    res = mid;
                    l = mid + 1;
                }
                else
                    r = mid - 1;
            }
            printf("%d\n", res);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值