题意:给出两个字符串的最大相同前缀。

思路:hash是要hash,不hash是不可能的。hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天。不要用数组储存hash值,会爆,开vector就行。

代码:

#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = +;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
vector<ull> ha[maxn];
int len[maxn];
char s[];
void HASH(int x){
len[x] = strlen(s + );
ha[x].clear();
ha[x].push_back();
for(int i = ; i <= len[x]; i++){
ha[x].push_back(ha[x][i - ] * seed + s[i]);
}
}
int main(){
int T, Case = ;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = ;i <= n;i++){
scanf("%s", s + );
HASH(i);
}
int q;
printf("Case %d:\n", Case++);
scanf("%d", &q);
while(q--){
int u, v;
scanf("%d%d", &u, &v);
int l = , r = min(len[u], len[v]), m, ans = ;
while(l <= r){
m = (l + r) / ;
if(ha[u][m] == ha[v][m]){
l = m + ;
ans = m;
}
else r = m - ;
}
printf("%d\n", ans);
}
}
return ;
}
/*
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
Sample Output
Case 1:
8
1
8
4
Case 2:
0
4
*/

UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解的更多相关文章

  1. UVa 11996 Jewel Magic (splay + Hash + 二分)

    题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令: 1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入2 p 删除第p个字符,后面的字符往前移3 p1 p2反转 ...

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

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

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

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

  4. UVA - 10029 Edit Step Ladders (二分+hash)

    Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...

  5. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  6. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  7. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  8. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  9. LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)

    题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...

  10. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

随机推荐

  1. LeetCode——Number of 1 Bits

    //求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...

  2. 微信Tinker的一切都在这里,包括源码(一)

    版权声明:本文由张绍文原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/101 来源:腾云阁 https://www.qclo ...

  3. jvm原理之内存机制

    转自:https://www.cnblogs.com/dreamowneryong/p/6381633.html JVM栈由堆.方法区,栈.本地方法栈.程序计数器等部分组成,结构图如下所示: 还有一张 ...

  4. 从零打造在线网盘系统之Struts2框架配置全解析

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  5. MySQL集群简介与配置详解

    1. 先了解一下你是否应该用MySQL集群. 减少数据中心结点压力和大数据量处理,采用把MySQL分布,一个或多个application对应一个MySQL数据库.把几个MySQL数据库公用的数据做出共 ...

  6. Visual Studio 2017正式版离线安装方法

    Visual Studio 2017 RTM正式版离线安装及介绍. 首先至官网下载:https://www.visualstudio.com/zh-hans/downloads/ VS 2017 正式 ...

  7. #pragma once含义及用法

    #pragma once是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次. #pragma once是编译器相关的,有的编译器支持,有的编译器不支持,具体 ...

  8. 。。。。。。不带http https : 不报错 spring boot elasticsearch rest

    ......不带http https  : 不报错 先telnet http://onf:8080/getES653/道路桥梁正在“理疗”%20这14条道路纳入市政中修 @GetMapping(&qu ...

  9. 史上最全的MonkeyRunner自动化测试从入门到精通(1)

    原文地址https://zhuanlan.zhihu.com/p/26043620 MonkeyRunner使用 #-*- coding:utf-8 –*- from com.android.monk ...

  10. docker——三剑客之Docker swarm

    Docker Swarm是Docker官方的三剑客项目之一,提供Docker容器集群服务,是Docker官方对容器云生态进行支持的核心方案.使用它,用户可以将多个Docker主机封装为单个大型的虚拟D ...