题意:给一个初始串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. python摸爬滚打之day022----模块(序列化操作)

    1.pickle 可以将我们python中的任意数据类型转化成bytes并写入到文件中.  同样也可以把文件中写好的bytes转换回我们python的数据. pickle可以直接序列化对象. clas ...

  2. excel将百分比数据转为数值格式

    由于于原给定的数据是百分比格式的, 所以先在excel中将数据格式改为数值 修改步骤: 单纯更改单元格格式为数值没用,先在空白单元格输入数值格式的1,复制该数字,选中要转换格式的数据, 右键 ---- ...

  3. varnish缓存系统基础知识

    缓存系统类型 1.页面缓存/pageCache     缓存静态资源(html js css image)  例如:varnish    squid 2.数据缓存/dataCache      缓存应 ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  5. Software Testing, Lab 1

    1.Install Junit(4.12), Hamcrest(1.3) with Eclipse 2.Install Eclemma with Eclipse 3.Write a java prog ...

  6. 彻底解决(Microsoft Visual C++ 14.0 is required)的步骤123

    之前要用协程gevent,安装pip install gevent包时遇到Microsoft Visual C++ 14.0 is required的报错提示,各种下载没有解决很头疼, 前两天安装sc ...

  7. windows将文件夹映射为虚拟磁盘

    subst X: e:123 将e盘下的123文件夹映射为x盘,123的容量即x盘容量 subst X: /t 删除映射的x盘

  8. 分治法——归并排序(mergesort)

    首先上代码. #include <iostream> using namespace std; int arr[11]; /*两个序列合并成一个序列.一共三个序列,所以用 3 根指针来处理 ...

  9. pascal中的xor,shr,shl,Int(),ArcTan(),copy,delete,pos和leftstr,RightStr等详解

    数学函数:Inc(i)使I:=I+1;Inc(I,b)使I:=I+b;Abs(x)求x的绝对值例:abs(-3)=3Chr(x)求编号x对应的字符. 例:Chr(65)=’A’chr(97)=’a’c ...

  10. python SyntaxError: Non-ASCII character '\xe6' in file

    [1]python程序执行报错 报错:SyntaxError: Non-ASCII character '\xe6' in file /tmp/788580473/main.py on line 7, ...