AC+DP练习
1.HDU 2222 Keywords Search
求目标串中出现了几个模式串。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define MSE(a,b) memset(a,b,sizeof(a))
#define REN(x) for(ted*e=fch[x];e;e=e->nxt)
#define REP(i,s,t) for(int i=s,__=t;i<=__;i++)
#define DWN(i,s,t) for(int i=s,__=t;i>=__;i--)
using namespace std;
const int maxn=+,inf=-1u>>,sig=;
struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root;
node*get(node*&x){return x?x:root;}node*tran(node*x,int&c){while(x&&!x->tx[c])x=x->fail;return get(get(x)->tx[c]);}
void initac(){
for(node*x=ac;x!=nodecnt;x++){
MSE(x->tx,NULL);x->fail=NULL;x->cnt=;
}nodecnt=ac;root=nodecnt++;return;
}
void insert(char*s){
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];
}x->cnt++;return;
}
void getfail(){
queue<node*>Q;REP(c,,sig-)if(root->tx[c])Q.push(root->tx[c]);
while(!Q.empty()){node*x=Q.front();Q.pop();
REP(c,,sig-)if(x->tx[c])x->tx[c]->fail=tran(x->fail,c),Q.push(x->tx[c]);
}return;
}
int find(char*s){
node*x=root;int ans=;for(int i=;s[i];i++){int c=s[i]-'a';
x=tran(x,c);ans+=x->cnt;x->cnt=;node*p=x->fail;while(p)ans+=p->cnt,p->cnt=,p=p->fail;
}return ans;
}
inline int read(){
int x=;bool sig=true;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';return sig?x:-x;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;static int buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
char s[maxn];int n,T;
int main(){
T=read();while(T--){
initac();n=read();REP(i,,n)scanf("%s",s),insert(s);
getfail();scanf("%s",s);write(find(s));ENT;
}
return ;
}
2.hiho 1036 Trie图
是否含有模式串
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define MSE(a,b) memset(a,b,sizeof(a))
#define REN(x) for(ted*e=fch[x];e;e=e->nxt)
#define REP(i,s,t) for(int i=s,__=t;i<=__;i++)
#define DWN(i,s,t) for(int i=s,__=t;i>=__;i--)
using namespace std;
const int maxn=+,inf=-1u>>,sig=;
struct node{node*tx[sig],*fail;int cnt;}ac[maxn],*nodecnt=ac,*root;
node*get(node*&x){return x?x:root;}node*tran(node*x,int&c){while(x&&!x->tx[c])x=x->fail;return get(x);}
void initac(){
for(node*x=ac;x!=nodecnt;x++){
MSE(x->tx,NULL);x->fail=NULL;x->cnt=;
}nodecnt=ac;root=nodecnt++;return;
}
void insert(char*s){
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];
}x->cnt++;return;
}
void getfail(){
queue<node*>Q;REP(c,,sig-)if(root->tx[c])Q.push(root->tx[c]);
while(!Q.empty()){node*x=Q.front();Q.pop();
REP(c,,sig-)if(x->tx[c]){
node*p=tran(x->fail,c);x->tx[c]->fail=get(p->tx[c]);
if(p->tx[c]&&p->tx[c]->cnt)x->tx[c]->cnt+=p->tx[c]->cnt;Q.push(x->tx[c]);
}
}return;
}
bool find(char*s){
node*x=root;int ans=;for(int i=;s[i];i++){int c=s[i]-'a';
x=get(tran(x,c)->tx[c]);if(x->cnt)return true;//node*p=x->fail;while(p){if(p->cnt)return true;p=p->fail;}
}return false;
}
inline int read(){
int x=;bool sig=true;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';return sig?x:-x;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;static int buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
char s[maxn];int n,T;
int main(){
initac();n=read();REP(i,,n)scanf("%s",s),insert(s);
getfail();scanf("%s",s);puts(find(s)?"YES":"NO");
return ;
}
AC+DP练习的更多相关文章
- hdu2825Wireless Password(ac+dp)
链接 状压dp+ac dp[i+1][next[j]][st|tt]表示第i+1长度结点为next[j]状态为st|tt的时候的ans; dp[i+1][next[j]][st|tt]+=dp[i][ ...
- BZOJ_1030_[JSOI2007]_文本生成器_(AC自动机+DP)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1030 给出一些单词,问长度为\(m\)的文章有多少文章中出现过任意一个或多个单词. 分析 文章 ...
- BZOJ 3530: [Sdoi2014]数数 [AC自动机 数位DP]
3530: [Sdoi2014]数数 题意:\(\le N\)的不含模式串的数字有多少个,\(n=|N| \le 1200\) 考虑数位DP 对于长度\(\le n\)的,普通套路DP\(g[i][j ...
- HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11569&courseid=0 给出目标串,每个子串和 ...
- 哈理工新生赛 马拉车+贪心 最大密度子图 AC自动机+DP
J.Symmys Time Limit: 1000 MS Memory Limit: 262144 K Total Submit: 50 (13 users) Total Accepted: 2 (2 ...
- [JSOI2009]密码 [AC自动机]
题面 bzoj luogu 首先看到这题就知道随便暴枚 只要是多项式算法都能过 先常规建AC自动机 注意被别的单词包含的单词没有存在的价值 剩余单词状压 大力dp f[长度][节点编号][状态] \( ...
- 沉迷AC自动机无法自拔之:[UVALive 4126] Password Suspects
图片加载可能有点慢,请跳过题面先看题解,谢谢 一看到这么多模式串就非常兴奋,又是\(AC\)自动机 题目就是要求:经过 \(n\) 个节点,把所有单词都遍历一遍的方案数,和那道题差不多嘛 所以这样设: ...
- 沉迷AC自动机无法自拔之:穿越广场 square
如标题所言,我已经沉迷于AC自动机无法自拔了... 这又是一道AC自动的题,红红火火恍恍惚惚 穿越广场 [问题描述] L 国的仪仗队要穿越首都广场了.首都广场可以看做是一块 N*M 的矩形网格,仪仗队 ...
- 51nod1254 最大子段和 V2 DP
---题面--- 题解: 表示今天做题一点都不顺.... 这题也是看了题解思路然后自己想转移的. 看的题解其实不是这道题,但是是这道题的加强版,因为那道题允许交换k对数. 因为我们选出的是连续的一段, ...
随机推荐
- html input 文本框的一些操作(限制输入...)
1.取消按钮按下时的虚线框 在input里添加属性值 hideFocus 或者 HideFocus=true 2.只读文本框内容 在input里添加属性值 readonly 3.防止退后清空的TEXT ...
- 解决win service 2003 IIS发布Gis网站后,访问地图服务出错,无法正常打开而且 事件查看器出现错误提示。
错误详情: 应用程序-特定 权限设置未将 COM 服务器应用程序(CLSID 为{379376DB-AEA6-40D1-9491-9345E61EF6BE})的 本地 激活 权限授予用户 NT AUT ...
- Echarts使用随笔(1)-Echarts中markPoint的使用(静态、动态)-effect
先看一段关于初始化Echart js的使用 myChart = echarts.init(document.getElementById('mainChart')); var o ...
- 在ASP中调用DLL的方法
.net的dll已经不是严格意义上的动态连接库了,而是一个类或者类库.它是不能直接在ASP.VB等其它的应用环境中使用的. 我们可以通过COM包装器(COM callable wrapper (C ...
- Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证
1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...
- What is SaaS?
SaaS, or Software as a Service, describes any cloud service where consumers are able to access softw ...
- Java学习----HashMap原理
1.HashMap的数据结构 数组的特点是:寻址容易,插入和删除困难:而链表的特点是:寻址困难,插入和删除容易.那么我们能不能综合两者的特性,做出一种寻址容易,插入删除也容易的数据结构?答案是肯定的, ...
- javaScript 的option触发事件
先说jquery的option触发事件,很方便 $("option:selected")//这样就能直接触发选择的option了 在JavaScript中就显得比较麻烦,其实< ...
- phpcms v9后台美化需要修改的部分整理
PHPcms后台登陆后的页面修改 Phpcms->modules->admin->templates->main.tpl.php 1,安全提示部分 <h6>< ...
- web版扫雷小游戏(二)
接上篇~~第一次写这种技术博客,发现把自己做的东西介绍出来还是一件脑力活,不是那么轻松啊,好吧,想到哪写到哪,流水记录之,待完成之后再根据大家的意见进行修改吧. 游戏实现 根据对扫雷游戏的体验和分析, ...