Anti-Rhyme Pairs - UVa 12338 哈希

Anti-Rhyme Pairs

Input: Standard Input

Output: Standard Output

 

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 Tcases.

 

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 Qlines 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                            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




题意:对于两个字符串,找到从头开始最长的相同的字符串长度。

思路:哈希后二分查找。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
vector<unsigned long long> snum[100010];
int slen[100010];
char s[100010];
void solve(int pos)
{ int len=strlen(s);
  slen[pos]=len;
  snum[pos].clear();
  snum[pos].push_back(0);
  for(int i=1;i<=len;i++)
   snum[pos].push_back(snum[pos][i-1]*2333+s[i-1]);
}
int compare(int a,int b)
{ int len=min(slen[a],slen[b]);
  int l=0,r=len;
  while(l<r)
  { int mid=(l+r+1)/2;
    if(snum[a][mid]==snum[b][mid])
     l=mid;
    else
     r=mid-1;
  }
  return l;
}
int main()
{ int T,t,n,m,i,j,k,a,b,ans;
  scanf("%d",&T);
  for(t=1;t<=T;t++)
  { printf("Case %d:\n",t);
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    { scanf("%s",s);
      solve(i);
    }
    scanf("%d",&m);
    while(m--)
    { scanf("%d%d",&a,&b);
      ans=compare(a,b);
      printf("%d\n",ans);
    }
  }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值