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),且它们是一对多的 ...
随机推荐
- 浅谈Storm流式处理框架(转)
Hadoop的高吞吐,海量数据处理的能力使得人们可以方便地处理海量数据.但是,Hadoop的缺点也和它的优点同样鲜明——延迟大,响应缓慢,运维复杂. 有需求也就有创造,在Hadoop基本奠定了大数据霸 ...
- Android数据库hibernate框架
说明 /** * YDL_Hibernate总结 <br/> * (一)支持功能: 1.自己主动建表,支持属性来自继承类:可依据注解自己主动完毕建表,而且对于继承类中的注解字段也支持自己主 ...
- MD5加密算法的实现
//////////////////////////////////////////////////////////////////// /* md5.h ...
- 使用Django创建简易Blog
网上看了个例子,但是自己却运行不同,最后终于知道了原因,记录下来.原来没有给settings.py里的INSTALLED_APPS添加blog.就像这样: 这是一个手把手的实例教程,本来学习笔记一样, ...
- 【c语言】模拟库函数strstr
// 模拟库函数strstr #include <stdio.h> #include <assert.h> const char* my_strstr(const char * ...
- LB 负载均衡的层次结构(转)
作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就可以直接访问到我们的应用了.但对于互联网应用,在你的应用和用户之间还隔着一层低调的或厚或薄的负载均衡层软件,它们不显山 ...
- UVALive - 4621 Cav 贪心 + 分析
题目大意:有一张洞穴地图,要在这个洞穴里面存放水,要求水不能碰到洞穴顶部.如今给出每一个位置的顶部位置和地面高度.问最多能够放多少水 解题思路:根据物理定理,每一段有水的连续区间,水位高度必须相等 所 ...
- Cocos2d-x3.1 粒子效果演示样例
这里把粒子的几种效果粘出来,以便以后使用 原文地址:http://blog.csdn.net/qqmcy/article/details/37511259 // // IntervalLayer.cp ...
- 上市ASCII 表省内发现!
表格来自,这里 扩展码表:
- C#中使用gRPC
C#中使用gRPC 我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目 ...