UVA - 12338 Anti-Rhyme Pairs (哈希)
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 (哈希)的更多相关文章
- UVA 12338:Anti-Rhyme Pairs(后缀数组+ST表)
[题目链接] click [题目大意] 给出一些字符串,询问查询任意两个字符串的最长公共前缀 [题解] 将字符串拼接,对拼接的字符串做后缀数组,对于查询的两个字符串, 只要在height数组上查询区间 ...
- UVA 12338 - Anti-Rhyme Pairs(后缀数组+RMQ)
UVA 12338 - Anti-Rhyme Pairs 题目链接 题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度 思路:把字符串排序,就能求出height和rank数组,然后利用R ...
- UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解
题意:给出两个字符串的最大相同前缀. 思路:hash是要hash,不hash是不可能的.hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天.不要用数 ...
- UVA - 12338 Anti-Rhyme Pairs 二分+hash
题目链接:传送门 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- UVA 11019 Matrix Matcher(哈希)
题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...
- 海量数据挖掘MMDS week2: 局部敏感哈希Locality-Sensitive Hashing, LSH
http://blog.csdn.net/pipisorry/article/details/48858661 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- Python入门笔记(10):字典
一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的关系.字典是Python唯一的映射类型. 扩展1:哈希表一种数据结构,值是根据相关的键进行数据存储的 ...
- 《转》python学习(9)字典
转自 http://www.cnblogs.com/BeginMan/p/3156960.html 一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的 ...
随机推荐
- CC ANUMLA(STL的运用)
题目连接:http://www.codechef.com/problems/ANUMLA 题意:给一个序列所有子集和(2^n个子集),复原这个序列... 如:0 1 1 2 2 3 3 4 原序列为1 ...
- JDBC数据库连接
JDBC是什么? Java Data Base Connectivity JDBC是: 以统一方式訪问数据库的API,能够訪问不论什么类型表列数据.特别是存储在关系数据中的数据.JDBC代表Java数 ...
- 【mysql】关于子查询的一个例子
假设表my_tbl包含三个字段a,b,c:现在需要查询表中列a的每个不同值下的列b为最小值的记录量. 比如表记录为: a b c 1 3 'cd' 2 3 'nhd' 1 5 'bg' ...
- sql语句查询数据库中的表名/列名/主键/自动增长值
原文地址:http://blog.csdn.net/pukuimin1226/article/details/7687538 ----查询数据库中用户创建的表 ----jsj01 为数据库名 sele ...
- UpdatePanel Repeater内LinkButton造成页面刷新问题
本意:UpdatePanel1内嵌的Repeater1中带有LinkButton1, 将由LinkButton1触发页面的UpdatePanel2更新,而不需要更新UpdatePanel1,当然也不需 ...
- Oracle 最简单的随系统自己主动启动
Oracle 最简单的随系统自己主动启动 俗话说用户是上帝,他们有时候提出一个问题很的简单,就仅仅须要一句话,一分钟就完事了.可是拿到我们DBA来说,可能至少得半个小时甚至半个月才干满足他的一句话.有 ...
- Linux 远程查看tomcat控制台
我现在只说如何看远程的tomcat控制台命令. 用远程登陆客户端登陆linux进入tomcat/logs/文件夹下键入指令:tail -f catalina.out ctrl + c 退出 这样就可 ...
- UVa 884 - Factorial Factors
题目:输出n!中素数因数的个数. 分析:数论.这里使用欧拉筛法计算素数,在计算过程中求解就可以. 传统筛法是利用每一个素数,筛掉自己的整数倍: 欧拉筛法是利用当前计算出的全部素数,乘以当前数字筛数: ...
- erlang集群IP及port管理
erlang集群是依靠epmd维护的,epmd是erlang集群节点间port映射的守护进程.负责维护集群内的节点连接.提供节点名称到IP地址及port的解析服务. epmd 自己定义port号 ep ...
- 设计模式——辛格尔顿(Singleton)
要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 单例模式属于设计模式 ...
