Anti-rhyme Pairs
Input: Standard Input
Output: Standard Output
Often words that rhyme also end in the same sequence of characters. We Use the Define the concept of a anti-rhyme. An anti-rhyme are a pair of words that has a similar beginning. The degree of anti-rhyme of a pair of words is further defined to being the length of the longest string S such that Both strings start with S. Thus, "arboreal" and "Arcturus" are a anti-rhyme pair of degree 2, while "chalkboard" and "overboard" is an anti-rhyme PA IR 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 is 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 中文版 characters (' A ' to ' Z '), whose length L was guaranteed to being less than or equal to . In every case it was 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 II integers i and J separated by Whit Espace (1≤i, j≤n).
Output
The output consists of T cases, each starting with a, and "case x:", where x indica TES the X-th case. There should is exactly Qlines after the for each case. Each of those Q lines should contain a integer that's 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
|
Test instructions: For two strings, find the longest identical string length from the beginning.
Idea: Hash after two-point lookup.
The AC code is as follows:
#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);}}