UVA - 12338 Anti-Rhyme Pairs (哈希)
来源:互联网 发布:铜铁铝的比热容数据 编辑:程序博客网 时间:2024/06/01 11:51
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 string S such that both strings start withS. 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 thei-th and thej-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 areT cases.
Each case starts with the number of strings N (1 ≤ N ≤ 105) on a line by itself. The followingN lines each contain a single non-empty string made up entirely of lower case English characters ('a' to 'z'), whose lengthL is guaranteed to be less than or equal to 10,000. In every case it is guaranteed thatN*L ≤ 106.
The line following the last string contains a single integer Q (1 ≤ Q ≤ 106), the number of queries. Each of theQ lines following contain a query made up of two integersi 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:”, whereX indicates the X-th case. There should be exactlyQ lines after that for each case. Each of thoseQ 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
题意:给你n个字符串,让你求给定的两个串的最长公共前缀
思路:预处理是无法达到时间要求的,那么我们可以先hash处理出每个字符串每个字符的哈希值(这个值是递增),然后就能二分的比较查询了
#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;}
- UVA - 12338 Anti-Rhyme Pairs (哈希)
- Anti-Rhyme Pairs - UVa 12338 哈希
- UVA 12338 Anti-Rhyme Pairs
- [UVa 12338] Anti-Rhyme Pairs (字符串哈希+二分)
- Uva 12338: Anti-Rhyme Pairs(Hash)
- UVA 12338 - Anti-Rhyme Pairs(后缀数组+RMQ)
- uva 12338 - Anti-Rhyme Pairs(后缀数组+RMQ)
- UVA 12338 Anti-Rhyme Pairs (字符串哈希+二分)
- UVA 12238 Anti-Rhyme Pairs(hash+二分)
- uva12338 Anti-Rhyme Pairs (字符串hash+二分)
- 每次询问求出两个字符串的最长公共前缀的长度 后缀数组+RMQ+lcp UVA 12338 - Anti-Rhyme Pairs
- uva12338 - Anti-Rhyme Pairs 询问最长公共前缀 HASH+二分
- UVA12338Anti-Rhyme Pairs(哈希+二分最长前缀)
- UVA 1566 - John(anti-Nim)
- 反调试(Anti-debug)
- 反面模式(Anti-pattern)
- pairs
- pairs
- JNI字段描述符“([Ljava/lang/String;)V”
- SGU242 最大流 容量限制为2
- QuickTimer Container(mov/mp4格式)
- vim使用教程
- CMD下使用javac和java命令需要注意的地方
- UVA - 12338 Anti-Rhyme Pairs (哈希)
- sscanf 和sprint 64位数据处理
- 如何查看和修改操作系统字符集
- opencv 图像类的转换
- 手工解包 8583报文
- obj this 的使用
- smart phonics
- 深入 char * ,char ** ,char a[ ] ,char *a[] 内核
- 黑马程序员_Java继承、多态、接口零碎知识总结