Anti-Rhyme Pairs
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);
}
}
}