uva 12338 - Anti-Rhyme Pairs

Often two words that rhyme also end in the same sequence of characters. We use this property to
define the concept 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 length of the longest string S
such that both strings start with S. Thus, “arboreal” and “arcturus” are an anti-rhyme pair of degree
2, while chalkboard and overboard are an 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 strings formed by the i-th and the j-th words from the list.


Input


Input consists of a number of test cases. The first line of input contains the number of test cases T
(T ≤ 35). Immediately following this line are T cases.
Each case starts with the number of strings N (1 ≤ N ≤ 105
) on a line by itself. The following N
lines each contain a single non-empty string made up entirely of lower case English characters (‘a’ to
‘z’), whose length L is guaranteed to be less than or equal to 10,000. In every case it is guaranteed
that N ∗ L ≤ 106
.
The line following the last string contains a single integer Q (1 ≤ Q ≤ 106
), the number of queries.
Each of the Q lines following contain a query made up of two integers i and j separated by whitespace

(1 ≤ i, j ≤ N).


Output


The output consists of T cases, each starting with a 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 that is the answer to the corresponding query in the input.


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


Sample Output


Case 1:
8
1
8
4
Case 2:
0

4


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char sh[100005][10005];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        printf("Case %d:\n",i);
        int m,n;
        scanf("%d",&m);

        for(int j=1;j<=m;j++)
            scanf("%s",sh[j]);
        scanf("%d",&n);
        for(int k=1;k<=n;k++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int l,s=0;
            l=strlen(sh[a])<strlen(sh[b])?strlen(sh[a]):strlen(sh[b]);
            //cout <<sh[a]<<"````"<<sh[b]<<endl;
            for(int j=0;j<l;j++)
                if(sh[a][j]==sh[b][j])s++;
                else break;
            printf("%d\n",s);
        }
        //cout<<"````````over``````"<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值