【题目链接】 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表)的更多相关文章

  1. SPOJ 687 Repeats(后缀数组+ST表)

    [题目链接] http://www.spoj.com/problems/REPEATS/en/ [题目大意] 求重复次数最多的连续重复子串的长度. [题解] 考虑错位匹配,设重复部分长度为l,记s[i ...

  2. POJ 3693 Maximum repetition substring(后缀数组+ST表)

    [题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...

  3. BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay

    BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...

  4. UVA10829 L-Gap Substrings(后缀数组+ST表)

    后缀数组+ST表. 代填的坑. \(Code\ Below:\) #include <bits/stdc++.h> #define ll long long using namespace ...

  5. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  6. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  7. 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分

    4310: 跳蚤 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 180  Solved: 83[Submit][Status][Discuss] De ...

  8. bzoj千题计划312:bzoj2119: 股市的预测(后缀数组+st表)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2119 题意:将给定数组差分后,求ABA形式的字串个数,要求|B|=m,|A|>0 1.后缀数 ...

  9. 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 ...

随机推荐

  1. auto-keras 测试保存导入模型

    # coding:utf-8 import time import matplotlib.pyplot as plt from autokeras import ImageClassifier# 保存 ...

  2. word 宏,脚本编程

    '脚本方式新建word 再新建文档,文档中输入字符串"你好" Dim wdapp As Word.Application Dim wddoc As Word.Document Se ...

  3. Python 源码学习之内存管理 -- (转)

    Python 的内存管理架构(Objects/obmalloc.c): _____ ______ ______ ________ [ int ] [ dict ] [ list ] ... [ str ...

  4. css文本垂直水平居中

    一.单行文本居中 .content{ height:100px; line-height:100px; text-align:center; border:1px solid red; } 效果图 二 ...

  5. python基础===open()文件处理使用介绍

    本文转自:Python open()文件处理使用介绍 1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, c ...

  6. Redis缓存Mysql模拟用户登录Java实现实例[www]

    Redis缓存Mysql模拟用户登录Java实现实例 https://jingyan.baidu.com/article/09ea3ede1dd0f0c0aede3938.html redis+mys ...

  7. 异步网络模块之aiohttp的使用(一)

    异步网络模块之aiohttp的使用(一) 平时我们也许用的更多的是requests模块,或者是requests_hml模块,但是他们都属于阻塞类型的不支持异步,速度很难提高,于是后来出现了异步的gre ...

  8. 更新T1表,要添加一个条件A,但T1表没有A字段

    可以这样 如果T2表有A字段,T1.T2表有共同字段B,可以通过T2表A字段为条件查得B字段 再用B字段为条件去更新T1表 update T1 set C = '1' where B in(selec ...

  9. centos安装VNC的方法

    https://help.aliyun.com/knowledge_detail/6698160.html(阿里云官方文档,但是官方文档有些地方是错的,我更正了下) ----------------- ...

  10. Git 分支 - 分支的新建

    https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA% ...