Codeforces Round #349 (Div. 1)E. Forensic Examination
题意:给一个初始串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的更多相关文章
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- 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] ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp
题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...
- 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 ...
- Codeforces Round #349 (Div. 2)
第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...
- 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 ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
随机推荐
- here文档 here doc EOF重定向
here文档 here doc EOF重定向 http://www.cnblogs.com/xiangzi888/archive/2012/03/24/2415077.html 在shell脚本程序 ...
- python 控制语句基础---->代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 | 作用域:作用的范围
# ### 代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 # ### 作用域:作用的范围 print(11) print(12) prin ...
- 三、UI开发之核心基础——约束(入门)
先学个新技能:添加图片控件Image View iOS的图片控件是ImageView,ImageView通过提前载入用户指定的图片资源来显示相应的图片. 所以图片控件的关键信息有3个: 1. Imag ...
- linux 下tftpf搭建
什么是TFTP服务 TFTP(Trivial File Transfer Protocol,简单文件传输协议) 是TCP/IP协议族中的一个用来在客户机与服务器之间进行 简单文件传输的协 ...
- [macOS] macOS下,VirtualBox安装CentOS7.4, 搭建nginx, mysql, PHP5.6&PHP7.1
准备工作 网络设置 相关教程:http://www.jianshu.com/p/e6ba699b5992 ifcfg-enp0s3配置 TYPE=Ethernet BOOTPROTO=dhcp DEF ...
- GDscript风格指南
(惯例感谢godot开发组~~·) 缩进 缩进类型:Tabs (编辑器默认) 缩进大小:4 (编辑器默认) 每个缩进级别必须大于包含它的代码块. 良好的: for i in range(10): pr ...
- zw量化交易·实盘操作·系列培训班
参见: <zw量化交易·实盘操作·系列培训班> http://blog.sina.com.cn/s/blog_7100d4220102w0q5.html
- Shell egrep
1.egrep是grep命令的扩展.grep使用需要脱义字符“\”.-E也可以满足. 2.正则参数. (). #任意一个任意字符. ()? #0或1个前面的字符. ()+ #1或多次的前面字符. () ...
- 值得收藏:一份非常完整的 MySQL 规范
一.数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割. 所有数据库对象名称禁止使用 MySQL 保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来). 数据库对象的命名要能 ...
- 自制操作系统Antz(5)——深入理解保护模式与进入方法
Antz系统更新地址: https://www.cnblogs.com/LexMoon/category/1262287.html Linux内核源码分析地址:https://www.cnblogs. ...