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 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 casesT (T ≤ 35). Immediately following this line are
T 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 integers
i 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:”, where
X indicates the X-th case. There should be exactlyQ lines after that for each case. Each of those
Q 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 (哈希)的更多相关文章

  1. UVA 12338:Anti-Rhyme Pairs(后缀数组+ST表)

    [题目链接] click [题目大意] 给出一些字符串,询问查询任意两个字符串的最长公共前缀 [题解] 将字符串拼接,对拼接的字符串做后缀数组,对于查询的两个字符串, 只要在height数组上查询区间 ...

  2. UVA 12338 - Anti-Rhyme Pairs(后缀数组+RMQ)

    UVA 12338 - Anti-Rhyme Pairs 题目链接 题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度 思路:把字符串排序,就能求出height和rank数组,然后利用R ...

  3. UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解

    题意:给出两个字符串的最大相同前缀. 思路:hash是要hash,不hash是不可能的.hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天.不要用数 ...

  4. UVA - 12338 Anti-Rhyme Pairs 二分+hash

    题目链接:传送门 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> ...

  5. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

  6. UVA 11019 Matrix Matcher(哈希)

    题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...

  7. 海量数据挖掘MMDS week2: 局部敏感哈希Locality-Sensitive Hashing, LSH

    http://blog.csdn.net/pipisorry/article/details/48858661 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  8. Python入门笔记(10):字典

    一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的关系.字典是Python唯一的映射类型. 扩展1:哈希表一种数据结构,值是根据相关的键进行数据存储的 ...

  9. 《转》python学习(9)字典

    转自 http://www.cnblogs.com/BeginMan/p/3156960.html 一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的 ...

随机推荐

  1. 算法起步之Dijkstra算法

    原文:算法起步之Dijkstra算法 友情提示:转载请注明出处[作者 idlear    博客:http://blog.csdn.net/idlear/article/details/19687579 ...

  2. 使用和制作patch文件

    使用和制作patch文件 发表时间: 2007-2-13 20:57    作者: superuser    来源: 迷茫人 字体: 小 中 大 | 打印 原文http://www.linuxsir. ...

  3. autoit 处理文件上传弹出框,并在JAVA中调用

    Java  代码 //定义exe 文件存放的绝对路径 File file2 = new File("."); String command = file2.getCanonical ...

  4. BZOJ 3112 Zjoi2013 防守战线 单纯形

    题目大意: 单纯形*2.. . #include <cmath> #include <cstdio> #include <cstring> #include < ...

  5. HDU4685 Prince and Princess 完美搭配+良好的沟通

    意甲冠军:今天,有n王子,m公主.现在给他们配对,与王子会嫁给一个男人,他喜欢.公主无法做出选择. 这标题去咬硬,还有一类似的题目poj1904.那个题目也是给王子与公主配对,但那个是王子公主各n个, ...

  6. C#的百度地图开发(二)转换JSON数据为相应的类

    原文:C#的百度地图开发(二)转换JSON数据为相应的类 在<C#的百度地图开发(一)发起HTTP请求>一文中我们向百度提供的API的URL发起请求,并得到了返回的结果,结果是一串JSON ...

  7. jquery下php与ajax的数据交换方式

    参考链接:http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2013/0122/11971.html 一.前台传递字符串变量,后台返回字符串变量( ...

  8. quick-cocos2d-x游戏开发【4】——加入文本

    文本的加入在quick中被封装在ui类中,它能够创建EditBox.菜单以及文本,文本总得来说能够创建TTF和BMFont两种. api对于它的说明非常具体.ui.newBMFontLabel(par ...

  9. swift 笔记 (十八) —— 扩展

    扩展 扩展能够让我们给一个已有的类.结构体.枚举等类型加入�新功能,包含属性和方法,甚至是构造器,下标,支持协议等等... 甚至是我们拿不到源码的类.结构体.枚举,我们依旧能够给它加扩展... 看到这 ...

  10. 第一章_servlet

    [Servlet与Jsp学习指南] *学习这servlet前.需要下载servlet-api.jar,MyEclipse给自己带来的需求javaEE3.0的天赋足以使用注解获得的版本号servlet ...