HDU 6138 Fleet of the Eternal Throne (后缀数组+字典树, 2017 Multi-Univ Training Contest 8)
来源:互联网 发布:手机淘宝明星店铺 编辑:程序博客网 时间:2024/06/02 06:01
Problem
n 行字符串(只含有小写字母)。m 组询问,每组询问给定 x, y ,求第 x 行与第 y 行字符串的最长公共子序列长度(当然,需要额外满足,取得的字符串必须是某行的前缀)。
Idea
貌似想复杂了,后缀数组+字典树过的。官方题解表示用 AC 自动机。
预处理维护一棵字典树,用于判断获得的公共子序列是否是某串的前缀,或该公共子序列的某个前缀是否是某串的前缀(不过多介绍)。
对于每次询问,将第 x 行与第 y 行的字符串拼接,两串中间插入任意非小写字母作为分割,得到新串 str
。利用后缀数组获取该串 str
的 height 数组,该数组表示两个字典序相邻的后缀的最长公共前缀的长度。(关于后缀数组的知识,请自行搜索 [2009 NOI 后缀数组-处理字符串的有力工具] 。)
简单思考即可得到,为获取最长公共子序列,只需比较任意相邻相关后缀的最大 height 值(当然,此处的两相邻后缀需分属不同的原串,若两个后缀同属于 x 行的串或 y 行的,显然是不行的)。
对于获得的可能导致最长公共子序列的相邻后缀最长公共前缀,凭此前缀到字典树中扫描,确定是否是某串前缀,或部分前缀是某串的前缀。维护最大值。
Code
#include<bits/stdc++.h>using namespace std;const int N = 100000 + 10;int T, n, m, x, y;char s[N];int st[N], len[N], str[N];const int maxn = 300000 + 10;#define F(x) ((x)/3+((x)%3==1?0:tb))#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)int wa[maxn],wb[maxn],wv[maxn],wu[maxn];int rnk[maxn],height[maxn], sa[maxn];int c0(int *r,int a,int b) { return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}int c12(int k,int *r,int a,int b){ if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1); return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];}void sort(int *r,int *a,int *b,int n,int m){ int i; for(i=0;i<n;i++) wv[i]=r[a[i]]; for(i=0;i<m;i++) wu[i]=0; for(i=0;i<n;i++) wu[wv[i]]++; for(i=1;i<m;i++) wu[i]+=wu[i-1]; for(i=n-1;i>=0;i--) b[--wu[wv[i]]]=a[i]; return;}void dc3(int *r,int *sa,int n,int m){ int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p; r[n]=r[n+1]=0; for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i; sort(r+2,wa,wb,tbc,m); sort(r+1,wb,wa,tbc,m); sort(r,wa,wb,tbc,m); for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++) rn[F(wb[i])] = c0(r,wb[i-1],wb[i]) ? p-1 : p++; if(p<tbc) dc3(rn,san,tbc,p); else for(i=0;i<tbc;i++) san[rn[i]]=i; for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3; if(n%3==1) wb[ta++]=n-1; sort(r,wb,wa,ta,m); for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i; for(i=0,j=0,p=0;i<ta && j<tbc;p++) sa[p] = c12(wb[j]%3,r,wa[i],wb[j]) ? wa[i++] : wb[j++]; for(;i<ta;p++) sa[p]=wa[i++]; for(;j<tbc;p++) sa[p]=wb[j++]; return;}void cal_height(int *r,int *sa,int n){ int i,j,k=0; memset(rnk, 0, 4*n+8); for(i=1;i<=n;i++) rnk[sa[i]]=i; for(i=0;i<n; height[rnk[i++]] = k ) for(k?k--:0,j=sa[rnk[i]-1]; r[i+k]==r[j+k]; k++);}const int Node_max = 200000 + 5;struct Node{ int next[30]; bool cnt;} Trie[Node_max];int Tsize; void Trie_insert(int idx) { int tmp = 0; char c; for(int i=0;i<len[idx];i++) { c = s[ st[idx]+i ]; if(!Trie[tmp].next[ c-'a' ]) { Trie[tmp].next[ c-'a' ] = ++Tsize; } tmp = Trie[tmp].next[ c - 'a' ]; Trie[tmp].cnt = 1; }}int Trie_query(int idx, int mxlen) { int tmp = 0, ans = 0; char c; for(int i=0;i<mxlen;i++) { c = s[idx+i]; if(!Trie[tmp].next[ c-'a' ]) return ans; tmp = Trie[tmp].next[ c-'a' ]; ans++; } return ans;} int main(){ scanf("%d", &T); while(T-- && scanf("%d", &n)!=EOF) { Tsize = 0; memset(Trie, 0, sizeof(Trie)); int cur = 0; for(int i=1;i<=n;i++) { scanf(" %s", s+cur); st[i] = cur; len[i] = strlen(s+cur); cur += len[i]; Trie_insert(i); } scanf("%d", &m); while(m--) { scanf("%d %d", &x, &y); for(int i=0;i<len[x];i++) str[i] = s[ st[x]+i ] - 'a' + 1; str[ len[x] ] = 27; for(int i=0;i<len[y];i++) str[ len[x]+i+1 ] = s[ st[y]+i ] - 'a' + 1; str[ len[x] + len[y] + 1 ] = 0; int tlen = len[x]+len[y] + 1; dc3(str, sa, tlen+1, 30); cal_height(str, sa, tlen); int mx = 0; for(int i=2;i<=tlen;i++) { if(height[i] <= mx) continue; if(sa[i-1] < len[x] && sa[i] > len[x]) mx = max(mx, Trie_query(st[x] + sa[i-1], height[i])); else if(sa[i-1] > len[x] && sa[i] < len[x]) mx = max(mx, Trie_query(st[x] + sa[i], height[i])); } printf("%d\n", mx); } }}
阅读全文
0 0
- HDU 6138 Fleet of the Eternal Throne (后缀数组+字典树, 2017 Multi-Univ Training Contest 8)
- HDU 6138 Fleet of the Eternal Throne(2017 Multi-University Training Contest 8)
- HDU 6138 Fleet of the Eternal Throne 后缀数组+字典树
- HDU 6138 Fleet of the Eternal Throne (2017多校8
- HDU 6138 Fleet of the Eternal Throne (2017多校8
- HDU6138 F Fleet of the Eternal Throne(字典树+后缀数组+二分)
- HDU 6138 Fleet of the Eternal Throne
- hdu 6138 Fleet of the Eternal Throne
- HDU 6138 Fleet of the Eternal Throne
- HDU 6138 Fleet of the Eternal Throne AC自动机||后缀数组
- HDU 6138 Fleet of the Eternal Throne ( AC自动机)
- hdu 6138 Fleet of the Eternal Throne(AC自动机)
- HDU 6138 Fleet of the Eternal Throne(AC自动机)
- hdu 6138 Fleet of the Eternal Throne (ac自动机)
- HDU 6138 Fleet of the Eternal Throne [AC自动机]
- HDU 6138 Fleet of the Eternal Throne (AC自动机)
- HDU 6138 Fleet of the Eternal Throne AC自动机
- HDU 6138 2017多校第八场1006 Fleet of the Eternal Throne :AC自动机
- I2C 小结
- PowerDesigner 导出表结构 到Excel
- 基于TCP的服务器与客户端的搭建
- 前端开发规范
- Navicat通过跳板机连接MySQL(2层跳转)
- HDU 6138 Fleet of the Eternal Throne (后缀数组+字典树, 2017 Multi-Univ Training Contest 8)
- kaldi学习第二天
- PHP入门(第五弹:变量)
- Java 工具类
- 树状数组模板
- 面试题18:树的子结构
- java基础之序列化与反序列化
- 面试总结:TCP三次握手、四次挥手
- 2017年8月14日提高组T1 跳跃