【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=4416

【题目大意】

  给出一个字符串,然后,给出一个字符串集合,问在该字符串中出现,且不在字符串集合中出现的子串总数。

【题解】

  将集合中所有的子串在自动机上跑,保存匹配到的位置的最长匹配,
  用于在parent树上计算每个位置的最长匹配,对于一个位置,
  如果不存在匹配,那么他对答案的贡献就是其value值,
  如果存在匹配且匹配长度小于其长度那么取其差作为答案的贡献。最后输出即可。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200005;
char s[N];
struct sam{
int p,q,np,nq,cnt,last,a[N][26],l[N],f[N];
sam(){cnt=0;last=++cnt;}
int val(int c){return l[c]-l[f[c]];}
void init(){
cnt=0;last=++cnt;
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
memset(u,0,sizeof(u));
}
void extend(int c){
p=last;np=last=++cnt;l[np]=l[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}int b[N],x[N],n;
void build(){
scanf("%s",s+1);
int len=strlen(s+1);n=len;
for(int i=1;i<=len;i++)extend(s[i]-'a');
for(int i=1;i<=cnt;i++)b[l[i]]++;
for(int i=1;i<=len;i++)b[i]+=b[i-1];
for(int i=1;i<=cnt;i++)x[b[l[i]]--]=i;
}int u[N];
void doit(){
scanf("%s",s+1);
int p=1,len=strlen(s+1),tmp=0;
for(int i=1;i<=len;i++){
int c=s[i]-'a';
if(a[p][c])p=a[p][c],tmp++,u[p]=max(u[p],tmp);
else{
while(p&&!a[p][c])p=f[p];
if(!p)p=1,tmp=0;
else tmp=l[p]+1,p=a[p][c],u[p]=max(u[p],tmp);
}
}
}
void solve(){
long long ans=0;
for(int i=cnt;i;i--){
if(u[x[i]]){
u[f[x[i]]]=max(u[x[i]],u[f[x[i]]]);
if(u[x[i]]<l[x[i]])ans+=l[x[i]]-u[x[i]];
}else ans+=val(x[i]);
}printf("%I64d\n",ans);
}
}sam;
int T,n,cas=1;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
sam.init();
sam.build();
for(int i=1;i<=n;i++)sam.doit();
printf("Case %d: ",cas++);
sam.solve();
}return 0;
}

  

HDU 4416 Good Article Good sentence(后缀自动机)的更多相关文章

  1. [hdu 4416]Good Article Good sentence

    最近几天一直在做有关后缀自动机的题目 感觉似乎对后缀自动机越来越了解了呢!喵~ 这题还是让我受益颇多的,首先搞一个后缀自动机是妥妥的了 可是搞完之后呢? 我们来观察 step 这个变量,每个节点的 s ...

  2. [hdu4416 Good Article Good sentence]后缀自动机SAM

    题意:给出串A和串集合B={B1,B2,...,Bn},求串A的所有不同子串中不是B中任一串的子串的数目. 思路:把A和B中所有字符串依次拼接在一起,然后构造后缀自动机,计算每个状态的R集合元素的最大 ...

  3. HDOJ 4416 Good Article Good sentence

    题解转自:http://blog.csdn.net/dyx404514/article/details/8807440 2012杭州网络赛的一道题,后缀数组后缀自己主动机都行吧. 题目大意:给一个字符 ...

  4. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

  5. HDU 5343 MZL's Circle Zhou 后缀自动机+DP

    MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

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

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

  7. HDU 6194 string string string(后缀自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个字符串求其出现恰好k次的子串数量 [题解] 对串建立AC自 ...

  8. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...

  9. BZOJ 4516: [Sdoi2016]生成魔咒 后缀自动机 性质

    http://www.lydsy.com/JudgeOnline/problem.php?id=4516 http://blog.csdn.net/doyouseeman/article/detail ...

随机推荐

  1. 超简单TAB切换

    <div class="tab-fbox2"> <ul class="title-list2 clearfix"> <li cla ...

  2. codeforces 13E . Holes 分块

    题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...

  3. winform跨线程

    在winform中经常用到多线程,那么用多线程对控件进行赋值和修改的时候呢会出现类似“该控件不是当前线程创建”的错误信息,在winform中两种办法: 1.在加载事件中写这句话,其作用呢就是线程的异步 ...

  4. fcntl记录锁

    #include<fcntl.h> int fcntl(fd,F_GETLK/F_SETLK/F_SETLKW,struct flock *flockptr); F_GETLK:测试flo ...

  5. selenium webdriver 学习笔记(二)

    selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...

  6. ubuntu下lamp环境配置及将window代码迁移至linux系统

    因为最近要用需要去实现项目中的一个功能,比较好的做法就是在http://i.cnblogs.com/EditPosts.aspx?opt=1linux中实现.所以最近就将自己的代码全部迁移到linux ...

  7. billing是如何的拆分的?

    在SD模块中,我们经常会考虑Billing分拆,分拆的标准如下: 一.根据Billing的字段项目进行分拆 在sap的标准系统中,系统会比较VBRK表的所有字段(也包含复制拆分组合标准字段ZUKRI) ...

  8. Navicat for mysql 导出sql文件 导入sql文件

    运行速度快 不受大小限制 安全高效

  9. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  10. ultravnc

    virsh attach-disk