题解转自:http://blog.csdn.net/dyx404514/article/details/8807440

2012杭州网络赛的一道题,后缀数组后缀自己主动机都行吧。

题目大意:给一个字符串S和一系列字符串T1~Tn,问在S中有多少个不同子串满足它不是T1~Tn中随意一个字符串的子串。

思路:我们先构造S的后缀自己主动机,然后将每个Ti在S的SAM上做匹配,类似于LCS,在S中的每个状态记录一个变量deep,表示T1~Tn,在该状态能匹配的最大长度是多少,将每个Ti匹配完之后,我们将S的SAM做拓扑排序,自底向上更新每个状态的deep,同一时候计算在该状态上有多少个子串满足题目要求。详细过程例如以下:

1:对于当前状态,设为p,设p的par为q,则更新q->deep为q->deep和p->deep中的较大值。

2:若p->deep<p->val,则表示在状态p中,长度为p->deep+1~p->val的子串不是T1~Tn中随意字符串的子串,所以答案加上p->val-p->deep。否则表示状态p中全部字串均不满足要求,跳过就可以。

(注意若p->deep==0,表示状态p中全部的子串均满足题目要求,可是答案不是加上p->val-0,而是加上 p->val-p->par->val,这表示状态p中的字符串个数,所以对于p->deep==0要特殊处理)

最后输出答案就可以。

Good Article Good sentence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2112    Accepted Submission(s): 587

Problem Description
In middle school, teachers used to encourage us to pick up pretty sentences so that we could apply those sentences in our own articles. One of my classmates ZengXiao Xian, wanted to get sentences which are different from that of others, because he thought the
distinct pretty sentences might benefit him a lot to get a high score in his article.

Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick
from Article A which don't belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help
him, won't you?
 
Input
The first line contains an integer T, the number of test data. 

For each test data

The first line contains an integer meaning the number of classmates.

The second line is the string A;The next n lines,the ith line input string Bi.

The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters 'a' to 'z'.
 
Output
For each case, print the case number and the number of substrings that ZengXiao Xian can find.
 
Sample Input
3
2
abab
ab
ba
1
aaa
bbb
2
aaaa
aa
aaa
 
Sample Output
Case 1: 3
Case 2: 3
Case 3: 1
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=110000; struct SAM_Node
{
SAM_Node *fa,*next[26];
int len,id,pos;
SAM_Node(){}
SAM_Node(int _len)
{
len=_len;
fa=0; memset(next,0,sizeof(next));
}
}; SAM_Node SAM_node[maxn*2],*SAM_root,*SAM_last;
int SAM_size; SAM_Node *newSAM_Node(int len)
{
SAM_node[SAM_size]=SAM_Node(len);
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} SAM_Node *newSAM_Node(SAM_Node *p)
{
SAM_node[SAM_size]=*p;
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} void SAM_init()
{
SAM_size=0;
SAM_root=SAM_last=newSAM_Node(0);
SAM_node[0].pos=0;
} void SAM_add(int x,int len)
{
SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
np->pos=len; SAM_last=np;
for(;p&&!p->next[x];p=p->fa)
p->next[x]=np;
if(!p)
{
np->fa=SAM_root;
return ;
}
SAM_Node *q=p->next[x];
if(q->len==p->len+1)
{
np->fa=q;
return ;
}
SAM_Node *nq=newSAM_Node(q);
nq->len=p->len+1;
q->fa=nq; np->fa=nq;
for(;p&&p->next[x]==q;p=p->fa)
p->next[x]=nq;
} char A[maxn],B[maxn];
int c[maxn*2],LCS[maxn*2];
SAM_Node *top[maxn*2]; int main()
{
int T_T,T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&T);
scanf("%s",A);
int len=strlen(A);
SAM_init();
for(int i=0;i<len;i++)
SAM_add(A[i]-'a',i+1); memset(c,0,sizeof(c));
memset(LCS,0,sizeof(LCS));
memset(top,0,sizeof(top)); for(int i=0;i<SAM_size;i++)
c[SAM_node[i].len]++;
for(int i=1;i<=len;i++)
c[i]+=c[i-1];
for(int i=0;i<SAM_size;i++)
top[--c[SAM_node[i].len]]=&SAM_node[i];
while(T--)
{
scanf("%s",B);
int len2=strlen(B);
int temp=0; SAM_Node *now=SAM_root; for(int i=0;i<len2;i++)
{
int x=B[i]-'a';
if(now->next[x])
{
temp++;
now=now->next[x];
LCS[now->id]=max(LCS[now->id],temp);
}
else
{
while(now&&!now->next[x])
now=now->fa;
if(now)
{
temp=now->len+1;
now=now->next[x];
LCS[now->id]=max(LCS[now->id],temp);
}
else
{
temp=0; now=SAM_root;
}
}
}
}
long long int ans=0;
for(int i=SAM_size-1;i>=1;i--)
{
SAM_Node *p=top[i];
if(LCS[p->id])
{
if(p->fa)
LCS[p->fa->id]=max(LCS[p->fa->id],LCS[p->id]);
if(LCS[p->id]<p->len)
{
ans+=p->len-LCS[p->id];
}
}
else
{
ans+=p->len-p->fa->len;
}
}
printf("Case %d: %I64d\n",cas++,ans);
}
return 0;
}

HDOJ 4416 Good Article Good sentence的更多相关文章

  1. HDU 4416 Good Article Good sentence(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4416 [题目大意] 给出一个字符串,然后,给出一个字符串集合,问在该字符串中出现,且不在字符串集合 ...

  2. [hdu 4416]Good Article Good sentence

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

  3. Good Article Good sentence HDU - 4416 (后缀数组)

    Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...

  4. Good Article Good sentence HDU - 4416 (后缀自动机)

    Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...

  5. HDOJ 3507 Print Article

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. HDU4416Good Article Good sentence(后缀自动机)

    Problem Description In middle school, teachers used to encourage us to pick up pretty sentences so t ...

  7. hdu4416 Good Article Good sentence (后缀数组)

    题意:问a串中有多少种字符串集合B中没有的连续子串. a的长度10^5,B中的总长度为不超过10^5. 解法:后缀数组题目:后缀数组能够非常easy算出来一个串中有多少种子串. 把a和B集合连起来.求 ...

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

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

  9. HDU 4416 (后缀自动机)

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

随机推荐

  1. MEF初体验之七:Using Catalogs

    MEF特性化编程模型的价值主张之一是通过catalogs动态发现部件的能力.Catalogs允许应用程序很容易地消费那些通过[Export]已经自我注册的exports. Assembly Catal ...

  2. C++ tree(1)

    建立与基本操作 .有关二叉树的相关概念,这里不再赘述,假设不了解二叉树相关概念,建议先学习数据结构中的二叉树的知识点 准备数据 定义二叉树结构操作中须要用到的变量及数据等. #define MAXLE ...

  3. 解决android模块化升级方法

    有关本机android升级版本必须是全apk更新安装,我们无法实现的一些模块化升级的解决思路: 本地人+web混合动力APP~ 查询详情,我们必须实现模块化升级,无论使用方法,我这样做.首页写在每个功 ...

  4. 【转】Oracle Outline使用方法及注意事项

    概要  Oracle Outline是用来保持SQL运行计划(execution plan)的一个工具. 我们能够通过outline工具防止SQL运行计划在数据库环境变更(如统计信息,部分參数等)而引 ...

  5. 《Programming Hive》读书笔记(一)Hadoop和hive环境搭建

    <Programming Hive>读书笔记(一)Hadoop和Hive环境搭建             先把主要的技术和工具学好,才干更高效地思考和工作.   Chapter 1.Int ...

  6. Vertica对于所计算的时间SQL声明大全

    词:强.大.所有,强烈推荐 SQL语句 查询结果 select (timestamp '2005-01-17 10:00' - timestamp '2005-01-01'); 16 10:10 se ...

  7. 国外流行的共享网站实现:facebook,twitter,google+1,tumblr等待

    近期需要做相关的国外几个站点共享,本来我以为它会和weibo.在同样的烦恼空间,什么appkey啦,apptoken啦.api啦.结果非常意外的发现并非如此恼火. Twitter分享: https:/ ...

  8. 前端javascript模板

    doT.js——前端javascript模板引擎问题备忘录 我手里维护的一个项目,遇到一个问题:原项目的开发人员在Javascript中,大量的拼接HTML,导致代码极丑,极难维护.他们怎么能够忍受的 ...

  9. css3 menu 手机菜单1

    首先看一下效果图; 效果1,主要是 translateY(100px) -->translateY(0px);opacity:0;—>opacity: 1; 然后递归延迟 怕麻烦也可以自己 ...

  10. Unity3D根据游戏的发展Terrain Toolkit地形生产

     今天我们继续给我Unity3D游戏开发系列.今天我们来通过Terrain Toolkit为了使地形. 虽然Unity3D它为我们提供了一个地形渲染工具,我们发现,这个地形绘制工具并不能满足我们的 ...