【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
给出 串和 个 串, 次询问,每次询问给出 、 、 、 ,求 在 中的哪一个里出现次数最多,输出出现次数最多的串编号(如果有多个则输出编号最小的)以及相应出现次数。
, 。
题解
广义后缀自动机+树上倍增+线段树合并
对 串和所有 串的反串放到一起建立广义后缀自动机,得到广义后缀树。
考虑 串的 部分在 串的出现次数体现为什么:" 串的 部分" 在后缀Trie上体现为:顺着 的以 开头的后缀走到 对应节点,该节点是子树内所有后缀的前缀。因此统计的就是该节点子树内有多少个 的后缀节点。
而现在给出的是后缀树,后缀树相比后缀Trie对无用节点进行压缩,有可能 是无用节点。因此要找到的是:最小的 ,使得 是非无用节点。使用倍增,从底到上求出最靠近根节点的 的节点。
问题转化为:求一个点的子树中出现次数最多的颜色是什么。
将询问离线,使用线段树维护子树(right集合)中每种颜色出现的次数,维护区间最大值即最大值位置。DFS整棵树,递归子树后进行线段树合并,最后处理该点对应的询问。
时间复杂度 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #define N 1100000 #define lson l , mid , ls[x] #define rson mid + 1 , r , rs[x] using namespace std; typedef pair< int , int > pr; vector< int > vq[N]; int m , pos[N] , c[N][26] , dis[N] , pre[N] , tot = 1 , last = 1 , head[N] , to[N] , next[N] , cnt , fa[N][22] , deep[N] , log [N] , ls[N * 5] , rs[N * 5] , root[N] , tp , ql[N] , qr[N]; pr mx[N * 5] , ans[N]; char str[N]; void extend( int x) { int p = last; if (c[p][x]) { int q = c[p][x]; if (dis[q] == dis[p] + 1) last = q; else { int nq = ++tot; memcpy (c[nq] , c[q] , sizeof (c[q])); dis[nq] = dis[p] + 1 , pre[nq] = pre[q] , last = pre[q] = nq; while (p && c[p][x] == q) c[p][x] = nq , p = pre[p]; } } else { int np = last = ++tot; dis[np] = dis[p] + 1; while (p && !c[p][x]) c[p][x] = np , p = pre[p]; if (!p) pre[np] = 1; else { int q = c[p][x]; if (dis[q] == dis[p] + 1) pre[np] = q; else { int nq = ++tot; memcpy (c[nq] , c[q] , sizeof (c[q])); dis[nq] = dis[p] + 1 , pre[nq] = pre[q] , pre[np] = pre[q] = nq; while (p && c[p][x] == q) c[p][x] = nq , p = pre[p]; } } } } inline void add( int x , int y) { to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt; } void dfs( int x) { int i; for (i = 1 ; i <= log [deep[x]] ; i ++ ) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for (i = head[x] ; i ; i = next[i]) fa[to[i]][0] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]); } int find( int x , int d) { int i; for (i = log [deep[x]] ; ~i ; i -- ) if ((1 << i) <= deep[x] && dis[fa[x][i]] >= d) x = fa[x][i]; return x; } inline void pushup( int x) { mx[x] = max(mx[ls[x]] , mx[rs[x]]); } void insert( int p , int l , int r , int &x) { if (!x) x = ++tp; if (l == r) { mx[x].first ++ , mx[x].second = -p; return ; } int mid = (l + r) >> 1; if (p <= mid) insert(p , lson); else insert(p , rson); pushup(x); } int merge( int l , int r , int x , int y) { if (!x) return y; if (!y) return x; if (l == r) { mx[x].first += mx[y].first; return x; } int mid = (l + r) >> 1; ls[x] = merge(l , mid , ls[x] , ls[y]); rs[x] = merge(mid + 1 , r , rs[x] , rs[y]); pushup(x); return x; } pr query( int b , int e , int l , int r , int x) { if (b <= l && r <= e) return mx[x]; int mid = (l + r) >> 1; if (e <= mid) return query(b , e , lson); else if (b > mid) return query(b , e , rson); else return max(query(b , e , lson) , query(b , e , rson)); } void solve( int x) { int i; for (i = head[x] ; i ; i = next[i]) solve(to[i]) , root[x] = merge(1 , m , root[x] , root[to[i]]); for (i = 0 ; i < ( int )vq[x].size() ; i ++ ) ans[vq[x][i]] = query(ql[vq[x][i]] , qr[vq[x][i]] , 1 , m , root[x]); } inline char nc() { static char buf[100000] , *p1 , *p2; return p1 == p2 && (p2 = (p1 = buf) + fread (buf , 1 , 100000 , stdin) , p1 == p2) ? EOF : *p1 ++ ; } inline int readnum() { int ret = 0; char ch = nc(); while (! isdigit (ch)) ch = nc(); while ( isdigit (ch)) ret = ((ret + (ret << 2)) << 1) + (ch ^ '0' ) , ch = nc(); return ret; } inline int readstr( char *p) { char ch = nc() , *q = p; while ( isalpha (ch)) *q ++ = ch , ch = nc(); return q - p; } char pbuf[10000000] , *pp = pbuf; inline void write( int x) { static int sta[20]; int top = 0; if (!x) sta[top ++ ] = 0; while (x) sta[top ++ ] = x % 10 , x /= 10; while (top -- ) *pp ++ = sta[top] ^ '0' ; } int main() { int q , i , j , x , y; for (i = readstr(str + 1) ; i ; i -- ) extend(str[i] - 'a' ) , pos[i] = last; m = readnum(); for (i = 1 ; i <= m ; i ++ ) { last = 1; for (j = readstr(str + 1) ; j ; j -- ) extend(str[j] - 'a' ) , insert(i , 1 , m , root[last]); } for (i = 2 ; i <= tot ; i ++ ) add(pre[i] , i) , log [i] = log [i >> 1] + 1; dfs(1); q = readnum(); for (i = 1 ; i <= q ; i ++ ) { ql[i] = readnum() , qr[i] = readnum() , x = readnum() , y = readnum(); vq[find(pos[x] , y - x + 1)].push_back(i); } solve(1); for (i = 1 ; i <= q ; i ++ ) write(ans[i].first ? -ans[i].second : ql[i]) , *pp ++ = ' ' , write(ans[i].first) , *pp ++ = '\n' ; fwrite (pbuf , 1 , pp - pbuf , stdout); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架