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. 最佳新秀SSH(十三)——Spring集装箱IOC分析和简单的实现

    时间最近一段时期,"集装箱"这个词一直萦绕在我的耳边,连吃饭.睡在我的脑海里蹦来蹦去的. 由于这几天的交流时间.讨论,对于理解容器逐渐加深. 理论上的东西终归要落实到实践,今天就借 ...

  2. 拿到阿里,网易游戏,腾讯,smartx的offer的过程 (转)

    前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...

  3. asp.net mvc4中自定义404页面

    原文地址:http://www.chuchur.com/asp-net-mvc4-404/ 定义404 方法当然有很多种.不同的方法所展现的形式也不一样,用户所体验也不一样.以下提供2两种 方法一: ...

  4. PHP_SELF、 SCRIPT_NAME、 REQUEST_URI差别

    $_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在使用方法上是很相似的,他们返回的都是与当前正在使用的页面地址有关 ...

  5. POJ2155:Matrix(二维树状数组,经典)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  6. Java8高中并发

    Java8中学并发 本文翻译自:http://jaxenter.com/lean-concurrency-in-java-8-49924.html 转载请注明出处:http://blog.csdn.n ...

  7. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  8. 外网SSH访问内网LINUX的N种方法

    外网SSH访问内网LINUX的N种方法 http://www.nat123.com/Pages_8_260.jsp 一,动态公网IP环境 1,环境描述: 路由器分配的是动态公网IP,且有路由管理权限, ...

  9. Windows Phone开发(43):推送通知第一集——Toast推送

    原文:Windows Phone开发(43):推送通知第一集--Toast推送 好像有好几天没更新了,抱歉抱歉,最近"光荣"地失业,先是忙于寻找新去处,唉,暂时没有下文.而后又有一 ...

  10. hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)

    上周的3*N的骨牌,因为状态只有8中,所以我们可以手算出状态转移的矩阵 但是这周是k*N,状态矩阵不好手算,都是我们改成用程序自动生成一个状态转移的矩阵就行了,然后用这个矩阵进行快速幂即可 枚举枚举上 ...