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+ ...
随机推荐
- sql暂时表的创建
create table #simple /*仅仅对当前用户有效.其它用户无法使用,断掉连接后马上销毁该表*/ ( id int not null ) select * from #simple c ...
- F - Humidex(1.4.2)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- LeetCode -- 最大连续乘积子序列
问题描写叙述: 给定数组,找出连续乘积最大值的子序列.比如 0,-1,-3.-2.则最大连续乘积为6= (-3) * (-2) 实现思路此题与最大连续和的子序列问题相似,也可通过找到递推公式然后用DP ...
- windows linux 双系统默认启动windows 的几种方法
装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,可是当想让XP作为默认启动的系统时怎么办呢? 在早期的Ubuntu系统 ...
- JAVA 解析复杂的json字符串
转自:https://blog.csdn.net/lovelovelovelovelo/article/details/73614473String parameter = { success : 0 ...
- Spring Security Architecture--官方
原文地址:https://spring.io/guides/topicals/spring-security-architecture/ Table of contents Authenticatio ...
- ivms4200 远程桌面访问测试过程及问题汇总
17.11.4 测试存储服务器配置后能否自动录像确认 10:34 4200客户端关闭 10:40 打开4200客户端软件 10:51 关机 10:56 开机,有提示出现,“防火墙阻止... ...
- weex入门(一)
emmmm其实没有接触过weex ,了解一番发现有很多坑,有很多基于weex改良后的框架,比如weexplus等等,基本不用踩多少坑.经过几天的深思熟虑我觉得还是去踩坑,毕竟踩完坑才能真正的了解嘛 w ...
- Centos7.6下安装Python3.7
前言 话说不会开发的运维不是一个好的DBA,所以我要开始学习python了,写博客记录一下我的学习过程,另外别欺负我新来的,那个每天更博的技术流ken是我哥. 不说了,时间宝贵,开整. 1.首先来看一 ...
- Tomcat 的三种高级运行模式
Tomcat 的连接器有两种:HTTP和AJP AJP(Apache JServ Protocol):AJP是面向数据包的基于TCP/IP的协议,它在Apache和Tomcat的实例之间提供了一个专用 ...