UVA 12338:Anti-Rhyme Pairs(后缀数组+ST表)
【题目链接】 click
【题目大意】
给出一些字符串,询问查询任意两个字符串的最长公共前缀
【题解】
将字符串拼接,对拼接的字符串做后缀数组,对于查询的两个字符串,
只要在height数组上查询区间最小值即可。
特别注意多组数据时候对字符串结尾的处理,很久没写容易忽视导致wa。
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=4000010;
int n,m,Rank[N],sa[N],h[N],tmp[N],cnt[N],ans;char t[N],s[N];
void suffixarray(int n,int m){
int i,j,k;n++;
for(i=0;i<2*n+5;i++)Rank[i]=sa[i]=h[i]=tmp[i]=0;
for(i=0;i<m;i++)cnt[i]=0;
for(i=0;i<n;i++)cnt[Rank[i]=s[i]]++;
for(i=1;i<m;i++)cnt[i]+=cnt[i-1];
for(i=0;i<n;i++)sa[--cnt[Rank[i]]]=i;
for(k=1;k<=n;k<<=1){
for(i=0;i<n;i++){
j=sa[i]-k;
if(j<0)j+=n;
tmp[cnt[Rank[j]]++]=j;
}sa[tmp[cnt[0]=0]]=j=0;
for(i=1;i<n;i++){
if(Rank[tmp[i]]!=Rank[tmp[i-1]]||Rank[tmp[i]+k]!=Rank[tmp[i-1]+k])cnt[++j]=i;
sa[tmp[i]]=j;
}memcpy(Rank,sa,n*sizeof(int));
memcpy(sa,tmp,n*sizeof(int));
if(j>=n-1)break;
}for(j=Rank[h[i=k=0]=0];i<n-1;i++,k++)
while(~k&&s[i]!=s[sa[j-1]+k])h[j]=k--,j=Rank[sa[j]+1];
}
int f[N][30],lg2[N];
void rmq_init(int n){
for(int i=2;i<=n;i++)lg2[i]=lg2[i/2]+1;
for(int i=1;i<=n;i++)f[i][0]=h[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
int rmq_min(int l,int r){
if(l>r)swap(l,r);l++;
int k=lg2[r-l+1];
return min(f[l][k],f[r-(1<<k)+1][k]);
}
int T,pos[N],len[N];
int main(){
scanf("%d",&T); int cas=1;
while(T--){
printf("Case %d:\n",cas++);
scanf("%d",&n); int u=1;
for(int i=1;i<=n;i++){
scanf("%s",&t);
len[i]=strlen(t);
pos[i]=u;
for(int j=0;j<len[i];j++)s[u++]=t[j];
}s[u]=0; //特别注意多组数据s[u]=0!!!!
suffixarray(u,256);
rmq_init(u); int q;
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
int ans=min(len[x],len[y]);
if(x==y)printf("%d\n",len[x]);
else printf("%d\n",min(ans,rmq_min(Rank[pos[x]],Rank[pos[y]])));
}
}return 0;
}
UVA 12338:Anti-Rhyme Pairs(后缀数组+ST表)的更多相关文章
- SPOJ 687 Repeats(后缀数组+ST表)
[题目链接] http://www.spoj.com/problems/REPEATS/en/ [题目大意] 求重复次数最多的连续重复子串的长度. [题解] 考虑错位匹配,设重复部分长度为l,记s[i ...
- POJ 3693 Maximum repetition substring(后缀数组+ST表)
[题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...
- BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay
BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...
- UVA10829 L-Gap Substrings(后缀数组+ST表)
后缀数组+ST表. 代填的坑. \(Code\ Below:\) #include <bits/stdc++.h> #define ll long long using namespace ...
- UVA 11475 Extend to Palindrome(后缀数组+ST表)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- bzoj千题计划312:bzoj2119: 股市的预测(后缀数组+st表)
https://www.lydsy.com/JudgeOnline/problem.php?id=2119 题意:将给定数组差分后,求ABA形式的字串个数,要求|B|=m,|A|>0 1.后缀数 ...
- Maximum repetition substring(POJ - 3693)(sa(后缀数组)+st表)
The repetition number of a string is defined as the maximum number \(R\) such that the string can be ...
随机推荐
- HDU 1070 Milk (模拟)
题目链接 Problem Description Ignatius drinks milk everyday, now he is in the supermarket and he wants to ...
- 大聊Python----IO口多路复用
什么是IO 多路复用呢? 我一个SocketServer有500个链接连过来了,我想让500个链接都是并发的,每一个链接都需要操作IO,但是单线程下IO都是串行的,我实现多路的,看起来像是并发的效果, ...
- javascript工厂模式、单例模式
//工厂模式 function createObject(name,age){ var obj = new Object(); obj.name = name; obj.age = age; obj. ...
- js基础知识点收集
js基础知识点收集 js常用基本类型 function show(x) { console.log(typeof(x)); // undefined console.log(typeof(10)); ...
- this可以通过call改变的测试
- Linux System.map文件【转】
转自:http://blog.csdn.net/ysbj123/article/details/51233618 当运行GNU链接器gld(ld)时若使用了"-M"选项,或者使用n ...
- [hadoop][会装]hadoop ha模式安装
1.简介 2.X版本后namenode支持了HA特性,使得整个文件系统的可用性更加增强. 2.安装前提 zookeeper集群,zookeeper的安装参考[hadoop][会装]zookeeper安 ...
- 关于那些Android中不常用的设置属性
很多在manifest中的属性我们经常遗忘了它们,或者经常看到但又不是很明白它的作用.那么在这里我就拿了一些属性简单的解释一下,防止以后碰到却不知道其中的意思.不是很全,以后会断断续续的补充吧 一.a ...
- HTML5晃动DeviceMotionEvent事件
关于devicemotion html5提供了几个新的DOM事件来获得设备物理方向及运动的信息,包括:陀螺仪.罗盘及加速计. 第一个DOM事件是**deviceorientation**,其提供设 ...
- java的装饰设计模式
类似python中的装饰器. 示例: public class Test5 { public static void main(String[] args) { Worker w = new Work ...