Description
D |
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 stringSSuch that both strings startS. 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 byI-Th andJ-Th words from the list.
Input
Input consists of a number of test cases. The first line of input contains the number of test casesT (T ≤ 35). Immediately following this line areTCases.
Each case starts with the number of stringsN (1 ≤ n ≤ 105)On a line by itself. The followingNLines each contain a single non-empty string made up entirely of lower case English characters ('A' to 'Z'), whose lengthLIs guaranteed to be less than or equal10,000. In every case it is guaranteed thatN * L ≤ 106.
The line following the last string contains a single integerQ (1 ≤ q≤106), The number of queries. Each ofQLines following contain a query made up of two integersIAndJSeparated by whitespace(1 ≤ I, j ≤ n).
Output
The output consistsTCases, each starting with a single line"Case X :", WhereXIndicatesX-Th case. There shocould be exactlyQLines after that for each case. Each of thoseQLines shoshould 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 |
Question: give you n strings and ask you for the longest public prefix of the given two strings
Idea: preprocessing cannot meet the time requirement, so we can first hash the hash value of each character in each string (this value is incremental), and then we can query the binary ratio.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 1000005;const int seed = 31;char str[maxn];int start[maxn], len[maxn];ll hash[maxn];int cal(int a, int b) {int l = 0, r = min(len[a], len[b]);while (l < r) {int m = (l + r + 1) >> 1;if (hash[start[a]+m-1] == hash[start[b]+m-1]) l = m;else r = m-1;}return l;}int main() {int t, n, cas = 1;scanf("%d", &t);while (t--) {scanf("%d", &n);int cur = 0;for (int i = 0; i < n; i++) {scanf("%s", str+cur);len[i] = strlen(str+cur);start[i] = cur;hash[cur] = str[cur] - 'a';for (int j = 1; j < len[i]; j++)hash[cur+j] = hash[cur+j-1] * seed + str[cur+j] - 'a';cur += len[i];}scanf("%d", &n);printf("Case %d:\n", cas++);int a, b;while (n--) {scanf("%d%d", &a, &b);printf("%d\n", cal(a-1, b-1));}}return 0;}
UV-12338 anti-rhyme pairs (hash)