WZJ的数据结构(负一)
难度级别:D; 运行时间限制:1000ms; 运行空间限制:262144KB; 代码长度限制:2000000B
试题描述
输入N个模板串Pi和文本串T,输出每个模板串Pi在T中出现了多少次。
输入
第一行为一个正整数N。
接下来N行为Pi。
最后一行为T
输出
输出N行,第i行为模板串Pi在T中出现的次数。
输入示例
5
a
ab
ba
aba
a
ababababa
输出示例
5
4
4
4
5
其他说明
1<=sigma(|Pi|)<=1000000
1<=|T|<=1000000 
保证Pi与T只由小写字母'a'或'b'组成

题解:感人至深!

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,inf=-1u>>,sig=,maxnode=maxn;
struct node{node*tx[sig],*fail;int x;node(){x=;}}ac[maxnode],*nodecnt=ac,*root=nodecnt++;
struct ted{int x,y;ted*nxt;}adj[maxn],*ms=adj,*fch[maxn],adj2[maxn],*ms2=adj2,*fch2[maxn];
void add(int x,int v){*ms=(ted){x,v,fch[x]};fch[x]=ms++;return;}
void add2(int x,int y){*ms2=(ted){x,y,fch2[x]};fch2[x]=ms2++;return;}
int num[maxn],ans[maxn];
void insert(char*s,int v){
node*x=root;
for(int i=;s[i];i++){
int c=s[i]-'a';
if(!x->tx[c]) x->tx[c]=nodecnt++;
x=x->tx[c];
} add(x-ac,v);return;
}
void getfail(){
queue<node*>Q;for(int c=;c<sig;c++)if(root->tx[c])Q.push(root->tx[c]),root->tx[c]->fail=root;
while(!Q.empty()){
node*x=Q.front();Q.pop();
for(int c=;c<sig;c++)if(x->tx[c]){
node*p=x->fail;while(p&&!p->tx[c])p=p->fail;if(!p)p=root;
x->tx[c]->fail=p->tx[c]?p->tx[c]:root;Q.push(x->tx[c]);
}
} return;
}
void dfs(node*x){
for(ted*e=fch2[x-ac];e;e=e->nxt) dfs(e->y+ac),num[x-ac]+=num[e->y];
}
int query(char*s){
for(int i=;i<=nodecnt-ac-;i++) add2(ac[i].fail-ac,i);
node*x=root;
for(int i=;s[i];i++){
int c=s[i]-'a';
while(x&&!x->tx[c]) x=x->fail;if(!x)x=root;
x=x->tx[c]?x->tx[c]:root;num[x-ac]++;
} dfs(root);
for(int i=nodecnt-ac-;i;i--)
for(ted*e=fch[i];e;e=e->nxt)
ans[e->y]=num[i];
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-;ch=getchar();}
while(isdigit(ch)) x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<) putchar('-'),x=-x;
int len=,buf[];while(x) buf[len++]=x%,x/=;
for(int i=len-;i>=;i--) putchar(buf[i]+'');return;
}
char s[maxn];
int n;
void init(){
n=read();
for(int i=;i<=n;i++) scanf("%s",s),insert(s,i);
getfail();
scanf("%s",s);query(s);
for(int i=;i<=n;i++) write(ans[i]),ENT;
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){
init();work();print();return ;
}

COJ 0999 WZJ的数据结构(负一)的更多相关文章

  1. COJ 0967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  2. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  3. COJ 0990 WZJ的数据结构(负十)

    WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...

  4. COJ 0995 WZJ的数据结构(负五)区间操作

    WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...

  5. COJ 0979 WZJ的数据结构(负二十一)

    WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...

  6. COJ 0981 WZJ的数据结构(负十九)树综合

    WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...

  7. COJ 1008 WZJ的数据结构(八) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...

  8. COJ 1007 WZJ的数据结构(七) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...

  9. COJ 1003 WZJ的数据结构(三)ST表

    WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...

随机推荐

  1. "解密"微信开放高级接口 企业如何应对

    今天(2013年10月29日)腾讯终于对外公开了微信公众平台最新的接口,一石激起千层浪,对于很多微信公众平台的运营人员来说,今天是令人兴奋的一天!微信在向申请服务号的企业开发了大量接口.用户不想输入文 ...

  2. uva 10555 - Dead Fraction)(数论)

    option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...

  3. 使用jcrop进行头像剪切

    http://www.cnblogs.com/chenssy/archive/2013/05/18/3084985.html http://code.ciaoca.com/jquery/jcrop/ ...

  4. unity3d优化IOS

    1. using UnityEngine; class GarbageCollectManager : MonoBehaviour {       public int frameFreq = 30; ...

  5. 触摸事件 Touch MotionEvent ACTION

    MotionEvent简介 当用户触摸屏幕时,将创建一个MontionEvent对象,MotionEvent包含了关于发生触摸的位置.时间信息,以及触摸事件的其他细节. 获取MontionEvent对 ...

  6. 95秀-PullToRefreshListView 示例

        正在加载.暂无数据页面 public class RefreshGuideTool {     private RelativeLayout rl_loading_guide;//整个View ...

  7. web02--jsp数据传递

    1.创建一个login.jsp登陆界面 <%@ page language="java" import="java.util.*" pageEncodin ...

  8. Python 的数据类型

    列表 #coding=utf-8 students=["小明","小华","小李","小娟","小云" ...

  9. TreeView控件之,后台构建TreeView(WinForm小程序)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX0AAAIdCAIAAABeBzrBAAAgAElEQVR4nOzdd5Qc130n+tl/nr3e5+

  10. Android NDK编程,引入第三方.so库

    android自带的编译工具NDK进行编译时(非单纯的调用第三方.so而是进行ndk编程),armeabi以及armeabi-v7a文件夹下的第三方so文件将会被删除,只会产生编译后的so文件,其他的 ...