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),且它们是一对多的 ...
随机推荐
- 离别·伤
天边露出尖尖的小月 青涩似梦 一点萤火虫落在时光的蘋 搜索 若然恍惚 莺归晚巢 日隐西山 至此予你别过 未曾听你轻启朱唇 未曾见你合身回眸 风,走过紫罗兰花 淡淡的香绕过你的长发 ...
- Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS
Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS Patch to solve sqlite3_ ...
- 在CodeBlocks 开发环境中配置使用OpenCV (ubuntu系统)
CodeBlocks是一个开放源代码的全功能的跨平台C/C++集成开发环境.CodeBlocks由纯粹的C++语言开发完毕,它使用了蓍名的图形界面库wxWidgets.对于追求完美的C++程序猿,再也 ...
- TreeSet排序
TreeSet的排序能够通过两种方法来实现: 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比較器进行排序. 2. ...
- HTML5之画布的拖拽/拖放
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> func ...
- js封装好的模仿qq消息弹窗代码
在我们的日常开发中,或者生活中.常常须要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 搜索树SVN的树的时候遇到的乱码问题
public void listDirectoryNode(SVNRepository repository, String dirUrl, FileNode node) { String curre ...
- MySQL命令行数据操作使用心得(总结版)
Char 0~255 Varchar 0~65535 text 0~65535(只能保存字符) Longtext 0~4294967295(只能保存字符) CMD登陆mysql mysql -u ro ...
- 安装ecshop提示“安装数据失败”或者“创建管理员帐号”
解决方法: 在install/includes/init.php文件的顶部,<?php 下增加: date_default_timezone_set ('Asia/Shanghai'); 即可 ...
- cocos2d-x2.x环境搭建配置
[安装工具] VS2012 Cocos2D-X 2.2.3 Python 2.7.8 一.运行cocos2dx中的hello world! 1.在Cocos2D-X 2.2.3目录下,点击cocos2 ...
