codeforces 666E. Forensic Examination(广义后缀自动机,Parent树,线段树合并)
解题思路:
很坑的一道题,需要离线处理,假如只有一组询问,那么就可以直接将endpos集合直接累加输出就好了。
这里就要将询问挂在树节点上,在进行线段树合并时查询就好了。
代码超级容易写挂的。
注意要将匹配串时尽量找到最浅根,易于统计答案。
倍增处理就好了。
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
const int N=;
struct int_2{
int vl;
int ps;
}imt;
struct sant{
int tranc[];
int len;
int pre;
}s[N];
struct pnt{
int hd;
int fa[];
int rt;
std::vector<int>posquery;
}p[N];
struct ent{
int twd;
int lst;
}e[N];
struct trnt{
int ls;
int rs;
int_2 val;
}tr[N<<];
struct qnt{
int l;
int r;
int pl;
int pr;
int lt;
int pos;
int_2 ans;
void Insert(void)
{
scanf("%d%d%d%d",&l,&r,&pl,&pr);
lt=pr-pl+;
return ;
}
}q[N];
char tmp[N];
char str[N];
std::vector<int>lpnt[N];
int n,m,Q;
int siz;
int fin;
int cnt;
int tnt;
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
return ;
}
int_2 max(int_2 x,int_2 y)
{
if(x.vl!=y.vl)
return x.vl>y.vl?x:y;
return x.ps<y.ps?x:y;
}
void pushup(int spc)
{
tr[spc].val=max(tr[tr[spc].ls].val,tr[tr[spc].rs].val);
return ;
}
void update(int l,int r,int &spc,int pos)
{
if(!spc)
spc=++tnt;
if(l==r)
{
tr[spc].val.vl++;
tr[spc].val.ps=pos;
return ;
}
int mid=(l+r)>>;
if(pos<=mid)
update(l,mid,tr[spc].ls,pos);
else
update(mid+,r,tr[spc].rs,pos);
pushup(spc);
return ;
}
void merge(int &spc1,int spc2)
{
if(!spc1||!spc2)
{
spc1|=spc2;
return ;
}
if(!tr[spc1].ls&&!tr[spc1].rs)
{
tr[spc1].val.vl+=tr[spc2].val.vl;
return ;
}
merge(tr[spc1].ls,tr[spc2].ls);
merge(tr[spc1].rs,tr[spc2].rs);
pushup(spc1);
return ;
}
int_2 query(int l,int r,int ll,int rr,int spc)
{
if(!spc||l>rr||ll>r)
return imt;
if(ll<=l&&r<=rr)
return tr[spc].val;
int mid=(l+r)>>;
return max(query(l,mid,ll,rr,tr[spc].ls),query(mid+,r,ll,rr,tr[spc].rs));
}
void Insert(int c)
{
int nwp,nwq,lsp,lsq;
nwp=++siz;
s[nwp].len=s[fin].len+;
for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
s[lsp].tranc[c]=nwp;
if(!lsp)
s[nwp].pre=;
else{
lsq=s[lsp].tranc[c];
if(s[lsq].len==s[lsp].len+)
s[nwp].pre=lsq;
else{
nwq=++siz;
s[nwq]=s[lsq];
s[nwq].len=s[lsp].len+;
s[nwp].pre=s[lsq].pre=nwq;
while(s[lsp].tranc[c]==lsq)
{
s[lsp].tranc[c]=nwq;
lsp=s[lsp].pre;
}
}
}
fin=nwp;
return ;
}
void Dfs(int x)
{
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
Dfs(to);
merge(p[x].rt,p[to].rt);
}
for(int i=;i<p[x].posquery.size();i++)
{
int h=p[x].posquery[i];
q[h].ans=query(,m,q[h].l,q[h].r,p[x].rt);
}
return ;
}
int main()
{
scanf("%s",str+);
n=strlen(str+);
scanf("%d",&m);
siz=;
for(int i=;i<=m;i++)
{
fin=;
scanf("%s",tmp+);
int tml=strlen(tmp+);
for(int j=;j<=tml;j++)
{
Insert(tmp[j]-'a');
update(,m,p[fin].rt,i);
}
}
for(int i=;i<=siz;i++)
ade(s[i].pre,i);
for(int i=;i<=siz;i++)
{
p[i].fa[]=s[i].pre;
}
for(int j=;j<=;j++){
for(int i=;i<=siz;i++)
{
p[i].fa[j]=p[p[i].fa[j-]].fa[j-]; }
}
scanf("%d",&Q);
for(int i=;i<=Q;i++)
{
q[i].Insert();
lpnt[q[i].pr].push_back(i);
}
int root=;
int lth=;
for(int i=;i<=n;i++)
{
int c=str[i]-'a';
while(root&&!s[root].tranc[c])
{
root=s[root].pre;
lth=s[root].len;
}
if(!root)
{
root=;
lth=;
continue;
}
root=s[root].tranc[c];
lth++;
for(int j=;j<lpnt[i].size();j++)
{
int h=lpnt[i][j];
int o=root;
if(lth<q[h].lt)
continue;
for(int k=;k>=;k--)
{
int nxt=p[o].fa[k];
if(s[nxt].len>=q[h].lt)
o=nxt;
}
p[o].posquery.push_back(h);
q[h].pos=o;
}
}
Dfs();
for(int i=;i<=Q;i++)
{
if(q[i].ans.vl==)
q[i].ans.ps=q[i].l;
printf("%d %d\n",q[i].ans.ps,q[i].ans.vl);
}
return ;
}
codeforces 666E. Forensic Examination(广义后缀自动机,Parent树,线段树合并)的更多相关文章
- 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...
- Codeforces.666E.Forensic Examination(广义后缀自动机 线段树合并)
题目链接 \(Description\) 给定串\(S\)和\(m\)个串\(T_i\).\(Q\)次询问,每次询问\(l,r,p_l,p_r\),求\(S[p_l\sim p_r]\)在\(T_l\ ...
- CF 666E Forensic Examination——广义后缀自动机+线段树合并
题目:http://codeforces.com/contest/666/problem/E 对模式串建广义后缀自动机,询问的时候把询问子串对应到广义后缀自动机的节点上,就处理了“区间”询问. 还要处 ...
- 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并
[CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...
- cf666E. Forensic Examination(广义后缀自动机 线段树合并)
题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...
- CF666E Forensic Examination 广义后缀自动机_线段树合并_树上倍增
题意: 给定一个串 $S$ 和若干个串 $T_{i}$每次询问 $S[pl..pr]$ 在 $Tl..Tr$ 中出现的最多次数,以及出现次数最多的那个串的编号. 数据范围: 需要离线 题解:首先,很常 ...
- CodeForces - 666E: Forensic Examination (广义SAM 线段树合并)
题意:给定字符串S,然后M个字符串T.Q次询问,每次给出(L,R,l,r),问S[l,r]在L到R这些T字符串中,在哪个串出现最多,以及次数. 思路:把所有串建立SAM,然后可以通过倍增走到[l,r] ...
- 【CF666E】Forensic Examination - 广义后缀自动机+线段树合并
广义SAM专题的最后一题了……呼 题意: 给出一个长度为$n$的串$S$和$m$个串$T_{1\cdots m}$,给出$q$个询问$l,r,pl,pr$,询问$S[pl\cdots pr]$在$T_ ...
- 【CF666E】Forensic Examination(后缀自动机,线段树合并)
[CF666E]Forensic Examination(后缀自动机,线段树合并) 题面 洛谷 CF 翻译: 给定一个串\(S\)和若干个串\(T_i\) 每次询问\(S[pl..pr]\)在\(T_ ...
- [BJWC2018]Border 的四种求法(后缀自动机+链分治+线段树合并)
题目描述 给一个小写字母字符串 S ,q 次询问每次给出 l,r ,求 s[l..r] 的 Border . Border: 对于给定的串 s ,最大的 i 使得 s[1..i] = s[|s|-i+ ...
随机推荐
- linux下u盘检測程序
获得U盘的插入或者拔取得信息的传统方法是在内核级执行hotplug程序.相关參数通过环境变量传递过来,再由hotplug通知其它关注hotplug的应用程序,可是效率比較低. ...
- mediawiki使用笔记
https://www.cnblogs.com/ToDoToTry/p/4475067.html
- 智课雅思词汇---四、clos和cap和ced是什么意思
智课雅思词汇---四.clos和cap和ced是什么意思 一.总结 一句话总结: cap/capt/cip/cep/ceiv:to take,seize(拿,抓住) cede:to go,yield( ...
- 10.ng-class-even与ng-class-odd
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS模板使你可以把该作用域内的数据直接绑定到所显示的HTML元素 ng-class-even与n ...
- Linux下配置Squid基础教程
Linux下配置Squid基础教程 本视频高清下载地址:http://down.51cto.com/data/437529 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处 ...
- Android CardView卡片布局 标签: 控件
CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...
- Win form碎知识点
判断1.ds不能为空 2.ds的表数量必须大于0 3.判断ds的第一个表中的行数必须有 if (ds.Tables.Count > 0 && ds != null &&a ...
- OpenSUSE下支持托盘的邮件客户端Sylpheed
在网上搜索了很多客户端想支持系统托盘,发现一个很不错的邮件客户端Sylpheed.设置方式和foxmail很像,最为重要的是支持系统托盘,很方便,默认没有开启,简单设置下:配置->通用首选项-& ...
- Linux samba服务器安装调试。
samba 做为主要的windown 和 Linux 通讯的服务器之一. 1. 查看是否安装了smb服务. #rpm -ga|grep samba 或者 #service smb status ...
- CMDB学习之八,完成所有资产采集信息的收集
#!/usr/bin/env python # -*- coding:utf-8 -*- import traceback from .base import BasePlugin from lib. ...