传送门:

解题思路:

很坑的一道题,需要离线处理,假如只有一组询问,那么就可以直接将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树,线段树合并)的更多相关文章

  1. 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并

    题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...

  2. Codeforces.666E.Forensic Examination(广义后缀自动机 线段树合并)

    题目链接 \(Description\) 给定串\(S\)和\(m\)个串\(T_i\).\(Q\)次询问,每次询问\(l,r,p_l,p_r\),求\(S[p_l\sim p_r]\)在\(T_l\ ...

  3. CF 666E Forensic Examination——广义后缀自动机+线段树合并

    题目:http://codeforces.com/contest/666/problem/E 对模式串建广义后缀自动机,询问的时候把询问子串对应到广义后缀自动机的节点上,就处理了“区间”询问. 还要处 ...

  4. 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并

    [CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...

  5. cf666E. Forensic Examination(广义后缀自动机 线段树合并)

    题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...

  6. CF666E Forensic Examination 广义后缀自动机_线段树合并_树上倍增

    题意: 给定一个串 $S$ 和若干个串 $T_{i}$每次询问 $S[pl..pr]$ 在 $Tl..Tr$ 中出现的最多次数,以及出现次数最多的那个串的编号. 数据范围: 需要离线 题解:首先,很常 ...

  7. CodeForces - 666E: Forensic Examination (广义SAM 线段树合并)

    题意:给定字符串S,然后M个字符串T.Q次询问,每次给出(L,R,l,r),问S[l,r]在L到R这些T字符串中,在哪个串出现最多,以及次数. 思路:把所有串建立SAM,然后可以通过倍增走到[l,r] ...

  8. 【CF666E】Forensic Examination - 广义后缀自动机+线段树合并

    广义SAM专题的最后一题了……呼 题意: 给出一个长度为$n$的串$S$和$m$个串$T_{1\cdots m}$,给出$q$个询问$l,r,pl,pr$,询问$S[pl\cdots pr]$在$T_ ...

  9. 【CF666E】Forensic Examination(后缀自动机,线段树合并)

    [CF666E]Forensic Examination(后缀自动机,线段树合并) 题面 洛谷 CF 翻译: 给定一个串\(S\)和若干个串\(T_i\) 每次询问\(S[pl..pr]\)在\(T_ ...

  10. [BJWC2018]Border 的四种求法(后缀自动机+链分治+线段树合并)

    题目描述 给一个小写字母字符串 S ,q 次询问每次给出 l,r ,求 s[l..r] 的 Border . Border: 对于给定的串 s ,最大的 i 使得 s[1..i] = s[|s|-i+ ...

随机推荐

  1. Android——bootchart

    bootchart:android原生自带的开机性能查看机制.通过收集android开机过程中的各种log数据,终于能够图表的形式展现各个进程在开机过程中的性能.(博客不能断-) 撰写不易,转载需注明 ...

  2. linux设备驱动归纳总结(三):4.ioctl的实现

    linux设备驱动归纳总结(三):4.ioctl的实现 一.ioctl的简单介绍: 尽管在文件操作结构体"struct file_operations"中有非常多相应的设备操作函数 ...

  3. 利用opencv源代码和vs编程序训练分类器haartraining.cpp

    如需转载请注明本博网址:http://blog.csdn.net/ding977921830/article/details/47733363. 一  训练框架 训练人脸检測分类器须要三个步骤: (1 ...

  4. java 返回json格式的数据

    1 阿里巴巴的fastjson import com.alibaba.fastjson.JSON; 使用的时候 JSON.toJSON(list); 2  Gson 解析json数据 import c ...

  5. SQL函数_Floor和Celling

    1 floor()函数用于获得小于或者等于数值表达式的最大整数,也就是向下取整;celling()函数用于获得大于或者等于数值表达式的最小整数,也就是向上取整: 2 floor()函数和round函数 ...

  6. cf 865 B. Ordering Pizza

    B. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...

  7. request获取各种路径总结、页面跳转总结。

    页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...

  8. Centos7部署phpMyAdmin系统

    phpMyAdmin是一个使用PHP语言编写,用来管理MYSQL数据库的Web应用系统 一:安装phpMyadmin 下载phpMyadmin最新版本4.8 wget https://files.ph ...

  9. SpringMVC框架的多表查询和增删查改

    必须声明本文章==>http://www.cnblogs.com/zhu520/p/7883268.html 一: 1):我的运行环境 我使用myeclipse(你也可以使用eclipse),t ...

  10. MFC- OnIdle空闲处理

    CWinApp::OnIdlevirtual BOOL OnIdle( LONG lCount );返回值: 如果要接收更多的空闲处理时间,则返回非零值:如果不需要更多的空闲时间则返回0.参数: lC ...