这里给出来一个后缀自动机的题解.

考虑对 $s$ 的正串和反串分别建后缀自动机.

对于正串的每个节点维护 $endpos$ 的最小值.

对于反串的每个节点维护 $endpos$ 的最大值.

这两个东西通过一个简单的基数排序就可以实现.

将 $p$ 的正串在正串的 SAM 上去匹配,一直
匹配到匹配不了为止,并记录 $p[i]$ 在正串中自动机节点上 $endpos$ 的最小值 $a[i]$.

对 $p$ 的反串也进行相同的处理,记录 $endpos$ 的最大值 $b[i]$.

正串中的 $endpos$ 就是 $p[1...i]$ 中 $i$ 的最小结束位置,那么反串中的 $endpos$ 就是 $p[i...length(p)]$ 中 $i$ 的最大开始位置.

所以,我们只需枚举 $1$~length(p) 并判断 $a[i]&&b[i]&&a[i]<b[i+1]$ 即可.

如果满足这个条件,就说明这个询问是有解的.

当然,要注意判断一下长度为 $1$ 的情况,这显然是无解的.

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200004
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
inline void getmin(int &a,int b) { if(b<a)a=b; }
inline void getmax(int &a,int b) { if(b>a)a=b; }
int a[1002],b[1002],n;
char str[N],P[N];
struct SAM
{
int c[N],rk[N],tot,last;
struct Node { int len,ch[27],f,minv,maxv; }t[N];
void init() { last=tot=1; }
inline void extend(int c,int lst)
{
int np=++tot,p=last;
last=np, t[np].len=t[p].len+1;
while(p&&!t[p].ch[c]) t[p].ch[c]=np,p=t[p].f;
if(!p) t[np].f=1;
else
{
int q=t[p].ch[c];
if(t[q].len==t[p].len+1) t[np].f=q;
else
{
int nq=++tot;
t[nq].len=t[p].len+1,t[nq].minv=t[nq].maxv=t[q].maxv;
memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
t[nq].f=t[q].f,t[q].f=t[np].f=nq;
while(p&&t[p].ch[c]==q) t[p].ch[c]=nq,p=t[p].f;
}
}
t[np].minv=t[np].maxv=lst;
}
inline void prepare()
{
int i,u;
for(i=1;i<=tot;++i) c[i]=0;
for(i=1;i<=tot;++i) ++c[t[i].len];
for(i=1;i<=tot;++i) rk[c[t[i].len]--]=i;
for(i=tot;i>=1;--i)
u=rk[i],getmin(t[t[u].f].minv,t[u].minv),getmax(t[t[u].f].maxv,t[u].maxv);
}
}t1,t2;
int main()
{
int i,j,m,re=0,ans=0;
// setIO("input");
t1.init(),t2.init();
scanf("%s",str+1),n=strlen(str+1);
for(i=1;i<=n;++i) t1.extend(str[i]-'A',i);
for(i=n;i>=1;--i) t2.extend(str[i]-'A',i);
t1.prepare(),t2.prepare(),scanf("%d",&m);
for(i=1;i<=m;++i)
{
int len,p;
scanf("%s",P+1),len=strlen(P+1),memset(a,0,sizeof(a)),memset(b,0,sizeof(b));
for(p=j=1;j<=len;++j)
{
int c=P[j]-'A';
if(t1.t[p].ch[c]) a[j]=t1.t[t1.t[p].ch[c]].minv,p=t1.t[p].ch[c]; else break;
}
for(p=1,j=len;j>=1;--j)
{
int c=P[j]-'A';
if(t2.t[p].ch[c]) b[j]=t2.t[t2.t[p].ch[c]].maxv,p=t2.t[p].ch[c]; else break;
}
re=0;
for(j=1;j<len;++j) {
if(a[j]&&b[j+1]&&a[j]<b[j+1]) re=1;
}
if(a[len]) re=1;
if(len==1) re=0;
ans+=re;
}
printf("%d\n",ans);
return 0;
}

  

CF 149E Martian Strings 后缀自动机的更多相关文章

  1. HDU 6208 The Dominator of Strings 后缀自动机

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  2. Codechef2015 May - Chef and Strings (后缀自动机)

    用后缀自动机统计出出现1~n次的串的数量f[i] 对于ans[k]=sigma(f[i]*C(i,k)) i>=k ; mo=; ..maxn] of dword; nt:..maxn,'a'. ...

  3. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  4. CodeForces 149E Martian Strings exkmp

    Martian Strings 题解: 对于询问串, 我们可以从前往后先跑一遍exkmp. 然后在倒过来,从后往前跑一遍exkmp. 我们就可以记录下 对于每个正向匹配来说,最左边的点在哪里. 对于每 ...

  5. CF 235C. Cyclical Quest [后缀自动机]

    题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...

  6. CF 452E. Three strings(后缀数组+并查集)

    传送门 解题思路 感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\).考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次, ...

  7. codeforces 149E . Martian Strings kmp

    题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...

  8. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  9. CodeForces - 616F:Expensive Strings (后缀自动机)

    You are given n strings ti. Each string has cost ci. Let's define the function of string , where ps, ...

随机推荐

  1. HDU 4585 Shaolin (STL map)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  2. Axios 的基本使用

    Axios 是一个基于 promise 的HTTP 库, 可以用在浏览器和 node.js 中. 1. 从浏览器创建 XMLHttpRequests 2. 从node.js 创建 http 请求 3. ...

  3. 安装ELK平台 7.3.0版本

    0. 事前准备工作 0.1 防火墙 若是使用公网IP的话可以考虑关闭防火墙,或者放行相应端口 使用内网IP的话可以不用管防火墙 0.2 关闭SElinux # setenforce 0 文件:/etc ...

  4. vim学习(二)之模式

    vim模式 基本上 vi/vim 共分为三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode). 命令模式: 用户刚刚启 ...

  5. 【测试环境】TCPCopy 使用方法

    https://blog.csdn.net/ronmy/article/details/65657691 TCPCopy是一种请求复制(所有基于tcp的packets)工具,可以把在线请求导入到测试系 ...

  6. J2EE WEB应用架构分析

    1. 架构概述 J2EE体系包括java server pages(JSP) ,java SERVLET, enterprise bean,WEB service等技术.这些技术的出现给电子商务时代的 ...

  7. 一、bif

    缩进是python的灵魂,缩进可以使python的代码整洁,有层次. python是脚本语言,就是为了简单方便以辅助科学运算,因此python有许多bif,build in function 全部都是 ...

  8. linux格式化磁盘命令

    linux格式化磁盘命令           linux mkfs         指令:mkfs 使用权限 : 超级使用者 使用方式 : mkfs [-V] [-t fstype] [fs-opti ...

  9. 有准备的面试才能拿到更好的 Offer

    http://www.sohu.com/a/331411917_181657 前几天有读者问我,工作不顺利辞职了.本来以为凭借自己的能力和工作经验可以轻松找到更好的工作,结果投了简历,约面试的很少,面 ...

  10. 2019.9.17更换ubuntu的镜像源 ubuntu安装lamp iis安装网站和ftp站

    更换ubuntu的镜像源 /etc/apt/sources.list cp  /etc/apt/sources.list  /etc/apt/sources.list.bak 备份这个文件 vim / ...