【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ 、$r$ 、$x$ 、$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r$ 中的哪一个里出现次数最多,输出出现次数最多的串编号(如果有多个则输出编号最小的)以及相应出现次数。
$|S|,q\le 5\times 10^5$ ,$\sum\limits_{i=1}^m|T_i|\le 5\times 10^4$ 。
题解
广义后缀自动机+树上倍增+线段树合并
对 $S$ 串和所有 $T_i$ 串的反串放到一起建立广义后缀自动机,得到广义后缀树。
考虑 $S$ 串的 $l...r$ 部分在 $T_i$ 串的出现次数体现为什么:" $S$ 串的 $l...r$ 部分" 在后缀Trie上体现为:顺着 $S$ 的以 $l$ 开头的后缀走到 $S_{l...r}$ 对应节点,该节点是子树内所有后缀的前缀。因此统计的就是该节点子树内有多少个 $T_i$ 的后缀节点。
而现在给出的是后缀树,后缀树相比后缀Trie对无用节点进行压缩,有可能 $S_{l...r}$ 是无用节点。因此要找到的是:最小的 $i\ge r$ ,使得 $S_{l...i}$ 是非无用节点。使用倍增,从底到上求出最靠近根节点的 $dis\ge r-l+1$ 的节点。
问题转化为:求一个点的子树中出现次数最多的颜色是什么。
将询问离线,使用线段树维护子树(right集合)中每种颜色出现的次数,维护区间最大值即最大值位置。DFS整棵树,递归子树后进行线段树合并,最后处理该点对应的询问。
时间复杂度 $O(26n+n\log n)$ 。
#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;
}
【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并的更多相关文章
- CF547E Milk and Friends(AC自动机的fail指针上建主席树 或 广义后缀自动机的parent线段树合并)
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...
- [BJWC2018]Border 的四种求法(后缀自动机+链分治+线段树合并)
题目描述 给一个小写字母字符串 S ,q 次询问每次给出 l,r ,求 s[l..r] 的 Border . Border: 对于给定的串 s ,最大的 i 使得 s[1..i] = s[|s|-i+ ...
- CF666E Forensic Examination 广义后缀自动机_线段树合并_树上倍增
题意: 给定一个串 $S$ 和若干个串 $T_{i}$每次询问 $S[pl..pr]$ 在 $Tl..Tr$ 中出现的最多次数,以及出现次数最多的那个串的编号. 数据范围: 需要离线 题解:首先,很常 ...
- 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并
[CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...
- CF 666E Forensic Examination——广义后缀自动机+线段树合并
题目:http://codeforces.com/contest/666/problem/E 对模式串建广义后缀自动机,询问的时候把询问子串对应到广义后缀自动机的节点上,就处理了“区间”询问. 还要处 ...
- cf666E. Forensic Examination(广义后缀自动机 线段树合并)
题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...
- 【CF666E】Forensic Examination - 广义后缀自动机+线段树合并
广义SAM专题的最后一题了……呼 题意: 给出一个长度为$n$的串$S$和$m$个串$T_{1\cdots m}$,给出$q$个询问$l,r,pl,pr$,询问$S[pl\cdots pr]$在$T_ ...
- CF1037H Security 后缀自动机 + right集合线段树合并 + 贪心
题目描述: 给定一个字符串 $S$ 给出 $Q$ 个操作,给出 $L,R,T$,求出字典序最小的 $S_{1}$ 为 $S[L...R]$的子串,且 $S_{1}$ 的字典序严格大于 $T$. 输出这 ...
- Codeforces.666E.Forensic Examination(广义后缀自动机 线段树合并)
题目链接 \(Description\) 给定串\(S\)和\(m\)个串\(T_i\).\(Q\)次询问,每次询问\(l,r,p_l,p_r\),求\(S[p_l\sim p_r]\)在\(T_l\ ...
随机推荐
- C++之强制类型转化
在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和dynamic_cast.这四个关键字都是用于强制类型转换的.我们逐一来介绍这四个关键字. ...
- AI1.1-人工智能史
来自:http://zh.wikipedia.org/wiki/人工智能史#CITEREFBerlinski2000 这篇是来自维基百科上面的人工智能史,将其大部分保留(真的是大部分,所以差不多没有原 ...
- Loopback接口用途---用作管理地址。
Loopback接口的优点是永远不会down点,不管是链路断开还是网卡损坏.因而loopback接口有很高健壮性. 但是loopback并非实际网口,外部设备要与该口通信,必须经过实际口的路由实现. ...
- tomcat-在eclispe中配置远程调试
在eclispe中新建web应用,名字叫webtest.里面只有一个HelloServlet.Web.xml配置如下. 修改tomcat的启动脚本startup.bat.复制startup.bat为s ...
- 2PC AND 3PC
一.分布式数据一致性 在分布式系统中,为了保证数据的高可用,通常会将数据保留多个副本(replica),这些副本会放置在不同的物理的机器上. (1)什么是数据一致性 在数据有多份副本的情况下,如果网络 ...
- 笔记:UITextView内容垂直居中方法
- (void)contentSizeToFit { //先判断一下有没有文字(没文字就没必要设置居中了) ) { //textView的contentSize属性 CGSize contentSiz ...
- C++STL之Vector的应用
这是我第一次写博客,请多指教! vector是一种向量容器,说白了就是可以改变大小的数组. vector是一个模板类,如果直接这样会报错: vector a; //报错,因为要指定模板. 需要像这样: ...
- MFC 如何为控件关联变量
所关联的变量常见有两种,一种就是控件变量,一种就是数字变量. 为控件关联变量的方法也有两种,一种是通过软件工具添加,一种是手动添加代码. 软件工具添加,方便简单,但是根据软件的版本不同,以及不同的空间 ...
- 【亲测有效】Win10家庭版Microsoft Edge页面出现乱码的两种解决方案及gpedit.msc命令无法使用的解决策略
昨天在爬取电影的时候生成的表单打开result.html时,发现页面出现如下乱码: 第一种方法: 上网找了半天,网上的解决方案是这样的: 1.Win + R输入gpedit.msc打开组策略编辑器; ...
- docker-compose 部署 MySql
信息: Docker版本($ docker --version):Docker版本18.03.1-ce,版本9ee9f40 系统信息:Windows10专业版 mysql挂载在Docker的volum ...