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

考虑对 $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. ubuntu/work

    1.Ubuntu16.04 创建桌面快捷方式 cd /usr/share/applications vim goland.desktop ##add content [Desktop Entry] E ...

  2. [转帖]影驰首发PCIe 4.0 SSD:群联AMD合作主控飚出5GB/s

    影驰首发PCIe 4.0 SSD:群联AMD合作主控飚出5GB/s https://www.cnbeta.com/articles/tech/851275.htm 硬件发展的真快.. AMD刚刚发布的 ...

  3. Java -cp命令的使用

    服务器跑程序,用到了一些Linux命令,做个简单笔记. Linux(Mac)下 java -cp .:jar包路径 主类的全限定名称     全限定名有绝对路径的意思,比如一个文件file的存放路径, ...

  4. 怎么编写properties文件

    1. 注释 在properties中注释是采用#号开头的方式来进行注释的 2. 编写properties文件 在properties中,一行就是一个键值对,简单的理解就是一行可以保存一个变量,键和值之 ...

  5. 786B - Legacy(线段树 + 最短路)线段树优化建图

    题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...

  6. synchronize和lock的区别 & synchionzie与volatile的区别

    synchronized与Lock的区别 https://www.cnblogs.com/iyyy/p/7993788.html Lock和synchronized和volatile的区别和使用 ht ...

  7. type=file 上传图片限制 类型和尺寸 方法

    <form> <input type="file" name="pic" id="pic" accept="im ...

  8. vue-cli解决兼容ie的es6+api问题

    官网:https://cli.vuejs.org/zh/guide/browser-compatibility.html#usebuiltins-usage https://github.com/vu ...

  9. 使用SQLyog对mysql数据库的数据结构进行导出、导入

    主要分为两个步骤: 一.使用SQLyog对mysql数据库的数据结构进行导出 1.鼠标右键选择需要导出数据结构的数据库——>点击[备份/导出]——>点击[备份数据库,转储到SQL....] ...

  10. 魔术矩阵Java代码

    //该魔术矩阵默认从右上角45度递增 //@漫流——595128841在qq点com //import java.util.Arrays; //用于打印API函数 public class 魔方矩阵 ...