BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)
Description
Input
Output
For each question, output the answer in one line.
Sample Input
abcabcabc
aaa
aafe
abc
a
ca
Sample Output
3
解题思路:
利用Parent树的子节点都是父节点母串的性质,在大串的每个实节点打上标记,然后构建出这颗Parent树。
那么我们的问题就变成了在一个Fail节点子树内不同颜色个数。
像不像HH的项链?
把Dfs序构建出来,离线树状数组就好了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=;
const int M=;
struct sant{
int tranc[];
int len;
int pre;
}s[N];
struct pnt{
int hd;
int col;
int ind;
int oud;
}p[N];
struct ent{
int twd;
int lst;
}e[N];
struct qust{
int l,r;
int ans;
int no;
}q[N];
int siz;
int fin;
int n,Q;
int cnt;
int dfn;
char tmp[N];
int line[N];
int lst[N];
int phr[N];
int col[N];
void Insert(int c,int whic)
{
int nwp,nwq,lsp,lsq;
nwp=++siz;
p[nwp].col=whic;
s[nwp].len=s[fin].len+;
for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
s[lsp].tranc[c]=nwp;
if(!lsp)
s[nwp].pre=;
else{
lsq=s[lsp].tranc[c];
if(s[lsq].len==s[lsp].len+)
s[nwp].pre=lsq;
else{
nwq=++siz;
s[nwq]=s[lsq];
s[nwq].len=s[lsp].len+;
s[nwp].pre=s[lsq].pre=nwq;
while(s[lsp].tranc[c]==lsq)
{
s[lsp].tranc[c]=nwq;
lsp=s[lsp].pre;
}
}
}
fin=nwp;
return ;
}
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
return ;
}
void Dfs(int x)
{
p[x].ind=++dfn;
phr[dfn]=x;
col[dfn]=p[x].col;
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
Dfs(to);
}
p[x].oud=dfn;
return ;
}
bool cmp(qust x,qust y)
{
return x.r<y.r;
}
bool cmq(qust x,qust y)
{
return x.no<y.no;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int pos,int x)
{
while(pos&&pos<=siz)
{
line[pos]+=x;
pos+=lowbit(pos);
}
return ;
}
int query(int pos)
{
int ans=;
while(pos)
{
ans+=line[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
fin=++siz;
scanf("%d%d",&n,&Q);
for(int i=;i<=n;i++)
{
scanf("%s",tmp+);
fin=;
int len=strlen(tmp+);
for(int j=;j<=len;j++)
Insert(tmp[j]-'a',i);
}
for(int i=;i<=siz;i++)
ade(s[i].pre,i);
Dfs(); for(int i=;i<=Q;i++)
{
scanf("%s",tmp+);
int root=;
int len=strlen(tmp+);
for(int j=;j<=len;j++)
root=s[root].tranc[tmp[j]-'a'];
q[i].no=i;
if(!root)
continue;
q[i].l=p[root].ind;
q[i].r=p[root].oud;
}
std::sort(q+,q+Q+,cmp);
int rr=;
for(int i=;i<=Q;i++)
{
while(rr<=q[i].r)
{
if(!col[rr])
{
rr++;
continue;
}
if(lst[col[rr]])
update(lst[col[rr]],-);
update(rr,);
lst[col[rr]]=rr;
rr++;
}
rr--;
if(!q[i].l)
continue;
q[i].ans=query(q[i].r)-query(q[i].l-);
}
std::sort(q+,q+Q+,cmq);
for(int i=;i<=Q;i++)
printf("%d\n",q[i].ans);
return ;
}
BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)的更多相关文章
- 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机
[BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other. But r ...
- BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]
JZPGYZ - Sevenk Love Oimaster Oimaster and sevenk love each other. But recently,sevenk hea ...
- bzoj 3277 串 && bzoj 3473 字符串 && bzoj 2780 [Spoj]8093 Sevenk Love Oimaster——广义后缀自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...
- BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster ——广义后缀自动机
给定n个串m个询问,问每个串在n个串多少个串中出现了. 构建广义后缀自动机,(就是把所有字符串的后缀自动机合并起来)其实只需要add的时候注意一下就可以了. 然后对于每一个串,跑一边匹配,到达了now ...
- 【BZOJ2780】【SPOJ】Sevenk Love Oimaster(后缀自动机)
[BZOJ2780][SPOJ]Sevenk Love Oimaster(后缀自动机) 题面 BZOJ 洛谷 题解 裸的广义后缀自动机??? 建立广义后缀自动机建立出来之后算一下每个节点被几个串给包括 ...
- BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】
题目 Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was dat ...
- BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)
题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
- SP8093 JZPGYZ - Sevenk Love Oimaster(广义后缀自动机)
题意 题目链接 Sol 广义后缀自动机板子题..和BZOJ串那个题很像 首先建出询问串的SAM,然后统计一下每个节点被多少个串包含 最后直接拿询问串上去跑就行了 #include<bits/st ...
- BZOJ2780——[Spoj]8093 Sevenk Love Oimaster
0.题意:给定N个原始字符串S,M次查询某个特殊的字符串S'在多少个原始串中出现过. 1.分析:这个题我们第一感觉就是可以用后缀自动机来搞,然后我们发现不是本质不同的字串..求出现过的次数,也就是说多 ...
随机推荐
- bzoj1066【SCOI2007】蜥蜴
1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2512 Solved: 1238 [Submit][Statu ...
- javascript小白学习指南1---0
第二章 变量和作用域 在看第二章时我希望,你能够回想一下前一次所讲的内容 假设有所遗忘 点这里 今天我们来说说 变量和作用域的问题 本章主要内容 基本类型和引用类型 运行环境 垃圾回收( ...
- iOS 7 UI 过渡指南 - 支持续 iOS 6(iOS 7 UI Transition Guide - Supporting iOS 6)
iOS 7 UI Transition Guide Preparing for Transition Before You Start Scoping the Project Supporting i ...
- 利用netstat和tasklist查看PC的端口占用情况 及80端口被占用
经常,我们在启动应用的时候发现系统需要的端口被别的程序占用,如何知道谁占有了我们需要的端口? 1.Windows平台在windows命令行窗口下执行: E:\oracle\ora92\bin>n ...
- 124.C++输出小结
#include <iostream> #include <iomanip> using namespace std; void main() { ////调用cout的成员函 ...
- ES6学习笔记(十)代理器Proxy
Java可以使用面向切面(AOP)的方法来实现某些统一的操作,比如某个操作的前置通知,后置通知等等,这种操作非常方便,其本质便是动态代理,JS的代理Proxy代理该如何使用呢? 某位大神的实现如下: ...
- POJ——T 2299 Ultra-QuickSort
http://poj.org/problem?id=2299 Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 62894 ...
- [React] Stop Memory Leaks with componentWillUnmount Lifecycle Method in React
In this lesson we'll take a stopwatch component we built in another lesson and identify and fix a me ...
- poj1328Radar Installation 贪心
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64472 Accepted: 14 ...
- POJ 2141 模拟
思路:字符串解密 啥都告诉你了 模拟就好 //By SiriusRen #include <cstdio> #include <cstring> using namespace ...