题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少

题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s_l-s_r\)子串的状态节点(先找到\(s_r\)对应的节点,然后倍增parent树即可),我们需要找到包含\(s_l-s_r\)的模式串,这些节点肯定在parent树上位于我们找到的状态节点的子树上.那么我们按sam的topo序进行线段树合并,线段树区间表示l,r模式串中最大匹配值以及下标.每次查询时先找初始串对应子串的状态节点,然后在该节点对应线段树中查询区间lr的最大匹配值.

需要注意的是此时线段树合并当xy子树都不为空时,不能直接把xy合并到x上,需要新开节点.因为查询是对于每个节点都可能的.

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=600000+10,maxn=1000000+10,inf=0x3f3f3f3f; int rt[N<<1],ls[N*20],rs[N*20],tot;
pii ma[N*20];
inline pii Max(pii a,pii b)
{
if(a.fi!=b.fi)return max(a,b);
return a.se < b.se ? a:b;
}
inline int Merge(int x,int y,int l,int r)
{
if(!x)return y;
if(!y)return x;
int o=++tot;
if(l==r)
{
ma[o]=mp(ma[x].fi+ma[y].fi,l);
return o;
}
int m=(l+r)>>1;
ls[o]=Merge(ls[x],ls[y],l,m);
rs[o]=Merge(rs[x],rs[y],m+1,r);
ma[o]=Max(ma[ls[o]],ma[rs[o]]);
return o;
}
void update(int &o,int pos,int l,int r)
{
if(!o)o=++tot;
if(l==r){ma[o]=mp(ma[o].fi+1,l);return ;}
int m=(l+r)>>1;
if(pos<=m)update(ls[o],pos,l,m);
else update(rs[o],pos,m+1,r);
ma[o]=Max(ma[ls[o]],ma[rs[o]]);
}
pii query(int o,int L,int R,int l,int r)
{
if(!o)return mp(0,0);
if(L<=l&&r<=R)return ma[o];
int m=(l+r)>>1;pii ans=mp(0,0);
if(L<=m)ans=Max(ans,query(ls[o],L,R,l,m));
if(m<R)ans=Max(ans,query(rs[o],L,R,m+1,r));
return ans;
}
char s[N];
struct SAM{
int last,cnt;
int ch[N<<1][27],fa[N<<1],l[N<<1];
int a[N<<1],c[N<<1],pos[N],f[N<<1][21];
void ins(int x)
{
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][x];
if(l[q]==l[p]+1)fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
void topo()
{
for(int i=1;i<=cnt;i++)c[l[i]]++;
for(int i=1;i<=cnt;i++)c[i]+=c[i-1];
for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i;
}
int go(int x,int len)
{
int y=pos[x];
for(int i=20;~i;i--)if(l[f[y][i]]>=len)y=f[y][i];
return y;
}
void build()
{
scanf("%s",s+1);
cnt=last=1;
int len=strlen(s+1);
for(int i=1;i<=len;i++)ins(s[i]-'a'),pos[i]=last;
int m;scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%s",s+1);
ins(26);int len=strlen(s+1);
for(int j=1;j<=len;j++)ins(s[j]-'a'),update(rt[last],i,1,m);
}
topo();
for(int i=cnt;i;i--)
{
f[i][0]=fa[i];
if(fa[a[i]])rt[fa[a[i]]]=Merge(rt[fa[a[i]]],rt[a[i]],1,m);
}
for(int i=1;i<=20;i++)for(int j=1;j<=cnt;j++)
f[j][i]=f[f[j][i-1]][i-1];
int q;scanf("%d",&q);
while(q--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
c=go(d,d-c+1);
pii te=query(rt[c],a,b,1,m);
printf("%d %d\n",max(a,te.se),te.fi);
}
}
}sam;
int main()
{
sam.build();
return 0;
}
/******************** ********************/

Codeforces Round #349 (Div. 1)E. Forensic Examination的更多相关文章

  1. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  2. Codeforces Round #349 (Div. 2) D. World Tour (最短路)

    题目链接:http://codeforces.com/contest/667/problem/D 给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] ...

  3. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  4. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  5. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  6. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #349 (Div. 2)

    第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...

  8. Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour   A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...

  9. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

随机推荐

  1. maven跳过单元测试-maven.test.skip和skipTests的区别

    1. 介绍 -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例, ...

  2. dbgrid 无法显示表中所有字段

    有时表中字段有更新,dbgrid无法显示表中字段. 解决办法: 1.adoquery断开 2. dbgrid字段全部删除 3.adoquery打开 4.dbgrid选择全部字段

  3. <c:forEach>详解

    <c:forEach>详解 <c:forEach>标签的语法定义如下所示. <c:forEach var="name" items="exp ...

  4. ASP.NET CORE 2.0 Uses SignalR Technology

    https://www.codeproject.com/Articles/1208322/ASP-NET-CORE-Uses-SignalR-Technology

  5. SQL SERVER 备份脚本

    DECLARE @FileName VARCHAR(200), @CurrentTime VARCHAR(50), @DBName VARCHAR(100), @SQL VARCHAR(1000)SE ...

  6. 使用msf对tomcat测试

    1.1 使用nmap命令对目标主机进行扫描.单击桌面空白处,右键菜单选择"在终端中打开". 1.2 在终端中输入命令"nmap –sV 192.168.1.3" ...

  7. laravel----------laravel5.3调度任务以及Artisan

    1.在使用的过程中会遇到有些函数不能用需要在php配置文件里面打开: disable_functions = exec,passthru,popen,proc_open,shell_exec,syst ...

  8. java.lang.ClassNotFoundException: com.sun.xml.ws.spi.ProviderImpl解决办法

    问题现象: 这种很可能出现在独立一个简单示例项目中可以用,但是在把webService模块加入系统后,报出这类错误. Exception in thread "main" java ...

  9. php 使用str_replace替换关键词(兼容字符串,一维数组,多维数组)

    通过递归的方式来实现替换字符串. /* * * 使用str_replace替换关键词(兼容字符串,一维数组,多维数组) * $search 需要查找的内容 * $replace 需要替换的内容 * $ ...

  10. phpstorm 配置git上传代码到 码云

    方法一: 1.安装git,一路next下去. git安装完自带右键菜单 2.看一下phpstorm里的路径是否正确. 3.使用phpstorm管理代码库 新建,从码云上已有的公开项目克隆一份到本地: ...