Anti-rhyme Pairs-uva 12338 Hash

Source: Internet
Author: User
Tags hash

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);}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.