原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html

题目传送门 - Codeforces 316G3

题意

  给定一个母串$s$,问母串$s$有多少本质不同的子串$t$是“好”的。

  一个字符串$t$是好的,仅当$t$满足了所有的$n$个条件。

  第$i$个条件用一个三元组$(p_i,L_i,R_i)$来描述。

  其中$p_i$为一个字符串,$L_i,R_i$为整数,且$L_i\leq R_i$。

  仅当字符串$t$在$p_i$中出现次数在$L_i$到$R_i$之间时,它是"好"的。

  $|s|,|p_i|\leq 5\times 10^4,n\leq 10$

题解

  考虑把输入的$n+1$个字符串用特殊字符隔开,并练成一个串。

  为了方便,我们将母串$s$放在第一个,$n$条规则中的字符串依次连续。

  我们定义数组$tot[i][j]$表示后缀自动机状态$i$的$Right$集合中有多少个位置处于第$j$个串。其中母串为第$0$个串,$p_i$为第$i$个串。

  这个可以通过基数排序+$dp$来搞定。

  然后我们分状态统计。

  对于状态$i$,如果$tot[i][0]=0$,那么说明这个状态所表示的一些串不存在于母串中,所以可以跳过。

  否则$tot[i][0]>0$,这个状态所表示的一些串存在于母串中。由于母串中没有特殊的字符,所以这个状态所表示的一些串也没有特殊字符,所以,对于已经得到的计数$tot[i][1\cdot n]$中也没有统计到包含特殊字符的子串。

  如果当前状态被计入,那么需要满足所有的$n$个条件,即$\forall 1\leq j\leq n,\ \ L_j\leq tot[i][j]\leq R_j$。当前状态包含的本质不同的串的个数显然为$Max(i)-Min(i)+1=Max(i)-Max(fa(i))$。加到答案里就可以了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=550005;
int n,m,L[15],R[15];
int size=1,root=1,last=1;
int tax[N<<1],id[N<<1];
LL tot[N<<1][12];
char s[N];
struct SAM{
int Next[27],fa,Max;
}t[N<<1];
void extend(int c,int id){
int p=last,np=++size,q,nq;
tot[np][id]++;
t[np].Max=t[p].Max+1;
for (;!t[p].Next[c];p=t[p].fa)
t[p].Next[c]=np;
q=t[p].Next[c];
if (t[q].Max==t[p].Max+1)
t[np].fa=q;
else {
nq=++size;
t[nq]=t[q],t[nq].Max=t[p].Max+1;
t[q].fa=t[np].fa=nq;
for (;t[p].Next[c]==q;p=t[p].fa)
t[p].Next[c]=nq;
}
last=np;
}
int main(){
t[0].Max=-1;
for (int i=0;i<27;i++)
t[0].Next[i]=1;
scanf("%s",s);
m=strlen(s);
for (int i=0;i<m;i++)
extend(s[i]-'a',0);
scanf("%d",&n);
for (int i=1;i<=n;i++){
extend(26,n+1);
scanf("%s%d%d",s,&L[i],&R[i]);
m=strlen(s);
for (int j=0;j<m;j++)
extend(s[j]-'a',i);
}
for (int i=1;i<=size;i++)
tax[t[i].Max]++;
for (int i=1;i<=size;i++)
tax[i]+=tax[i-1];
for (int i=1;i<=size;i++)
id[tax[t[i].Max]--]=i;
LL ans=0;
for (int i=size;i>=2;i--){
int x=id[i];
for (int j=0;j<=n;j++)
tot[t[x].fa][j]+=tot[x][j];
if (tot[x][0]==0)
continue;
bool flag=1;
for (int j=1;flag&&j<=n;j++)
flag&=L[j]<=tot[x][j]&&tot[x][j]<=R[j];
if (flag)
ans+=t[x].Max-t[t[x].fa].Max;
}
printf("%I64d",ans);
return 0;
}

  

Codeforces 316G3 Good Substrings 字符串 SAM的更多相关文章

  1. Codeforces 1276F - Asterisk Substrings(SAM+线段树合并+虚树)

    Codeforces 题面传送门 & 洛谷题面传送门 SAM hot tea %%%%%%% 首先我们显然可以将所有能够得到的字符串分成六类:\(\varnothing,\text{*},s, ...

  2. Codeforces 235C Cyclical Quest 字符串 SAM KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 -  CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...

  3. Codeforces 452E Three strings 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...

  4. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  5. Codeforces 700E. Cool Slogans 字符串,SAM,线段树合并,动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF700E.html 题解 首先建个SAM. 一个结论:对于parent树上任意一个点x,以及它所代表的子树内任 ...

  6. Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串

    D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...

  7. codeforces #271D Good Substrings

    原题链接:http://codeforces.com/problemset/problem/271/D 题目原文: D. Good Substrings time limit per test 2 s ...

  8. 【Codeforces 1037H】Security(SAM & 线段树合并)

    Description 给出一个字符串 \(S\). 给出 \(Q\) 个操作,给出 \(L, R, T\),求字典序最小的 \(S_1\),使得 \(S^\prime\) 为\(S[L..R]\) ...

  9. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

随机推荐

  1. corba/ice/web service/com+

    //todo model1 model2

  2. python ctypes

    official tutorial for ctypes libhttps://docs.python.org/3/library/ctypes.html 1 ctypes exports the c ...

  3. login_code

    #! -*- coding:utf-8 -*-"""http://www.cnblogs.com/weke/articles/6271206.html前置和后置1.set ...

  4. Confluence 6 启用 HTTP 压缩

    在屏幕的右上角单击 控制台按钮 ,然后选择 基本配置(General Configuration) 链接. 在左侧的面板中选择 通用配置(General Configuration). 启用 HTTP ...

  5. npm无反应的问题&npm常用命令

    RT: windows安装完nodejs后做了相关环境变量配置后,cmd输入npm命令无反应,就光标一直闪,百度了半天终于找到解决办法 解决方法:C:\Users\Administrator(或你的账 ...

  6. java String正则表达式

    1.正则表达式 字符串替换,     例子; String s="131hello334thrid  ".replaceAll("[a-zA-Z]"," ...

  7. Nginx详解十四:Nginx场景实践篇之代理服务

    代理的作用 Nginx代理 正向代理 反向代理 正向代理和反向代理的区别:代理的对象不一样 正向代理代理的对象是客户端,反向代理代理的对象是服务端 反向代理: 配置语法:proxy_pass URL; ...

  8. C++ LocalAlloc() & LocalSize() & LocalFree ()

    关于LocalAlloc function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85). ...

  9. RESTful架构解读

    什么是REST REST与技术无关,代表的是一种软件架构风格.REST全称是Representational State Tranfer, 表征性状态转移. REST从资源的角度类审视整个网络,它将分 ...

  10. jQuery 选择器demo练习

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...