我的第一道ac自动机+dp
学了那么久ac自动机,直到现在才会把它和dp结合在一起
结合在一起也没以前感觉的那么神奇,实际上就是图论中的记忆化搜索,ac自动机是一个图,在这个图上进行dp过程就叫记忆化搜索,和树形dp也挺像的~
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int mod = 20090717;
const int maxn = 121;
const int maxc = 26;
const int hashsize = 1<<10;
const int head = 0;
struct zz
{
short to[26];
short fail;
short id;
}zx[maxn];
struct aa
{
int len;
int pos;
int s;
}as;
int n,m,k,use;
string s;
int dp[maxc][maxn][hashsize+1]; // len | now | state
bool ok[maxn];
int fs[maxn];
bool hash[maxc][maxn][hashsize+1];
char ss[12];
inline int get()
{
use++;
zx[use].fail = 0;
zx[use].id = -1;
MM(zx[use].to,-1);
return use;
}
void insert(int id,string& p)
{
int now = head;
int c;
for(int i=0;i<p.length();i++)
{
c = p[i] - 'a';
if(zx[now].to[c] != -1)
{
now = zx[now].to[c];
}
else
{
zx[now].to[c] = get();
now = zx[now].to[c];
}
}
if(zx[now].id == id)
{
assert(false);
}
zx[now].id = id;
return ;
}
void ac()
{
queue<int>q;
CL(q);
q.push(head);
int now,to,tmp;
while(!q.empty())
{
now = q.front();
q.pop();
for(int u=0;u<maxc;u++) if(zx[now].to[u] != -1)
{
to = zx[now].to[u];
q.push(to);
tmp = now;
while(tmp)
{
tmp = zx[tmp].fail;
if(zx[tmp].to[u]!=-1)
{
zx[to].fail = zx[tmp].to[u];
break;
}
}
}
}
return ;
}
int find(int now)
{
if(ok[now])
{
return fs[now];
}
else
{
int re = 0;
ok[now] = true;
if(now)
{
if(zx[now].id != -1)
{
re |= 1<<zx[now].id;
}
re |= find(zx[now].fail);
}
fs[now] = re;
return re;
}
}
void dps()
{
dp[0][0][0] = 1;
as.len = 0;
as.pos = 0;
as.s = 0;
queue<aa>q;
CL(q);
q.push(as);
hash[0][0][0] = true;
aa now,to;
int temp;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0;i<26;i++)
{
if(zx[now.pos].to[i] != -1)
{
to.pos = zx[now.pos].to[i];
to.s = find(to.pos) | now.s;
to.len = now.len + 1;
}
else
{
temp = now.pos;
while(temp)
{
temp = zx[temp].fail;
if(zx[temp].to[i] != -1)
{
temp = zx[temp].to[i];
break;
}
}
zx[now.pos].to[i] = temp;
to.len = now.len+1;
to.pos = temp;
to.s = find(temp) | now.s;
}
if(to.len <= n)
{
dp[to.len][to.pos][to.s] += dp[now.len][now.pos][now.s];
dp[to.len][to.pos][to.s] %= mod;
if(!hash[to.len][to.pos][to.s])
{
hash[to.len][to.pos][to.s] = true;
q.push(to);
}
}
}
}
return ;
}
int has(int temp)
{
int re = 0;
for(int i=0;i<m;i++)
{
if(temp & (1<<i))
{
re++;
}
}
return re;
}
int start()
{
ac();
for(int i=0;i<=n;i++)
{
for(int j=0;j<=use;j++)
{
for(int k=0;k<=(1<<m);k++)
{
dp[i][j][k] = 0;
hash[i][j][k] = false;
}
}
}
MM(ok,false);
dps();
int re = 0;
for(int i=0;i<=use;i++)
{
for(int j=0;j<(1<<m);j++)
{
if(has(j)>=k)
{
re += dp[n][i][j];
re %= mod;
}
}
}
return re;
}
int main()
{
while(cin>>n>>m>>k)
{
if(!n && !m & !k) break;
use = -1;
get();
for(int i=0;i<m;i++)
{
// cin>>s;
scanf("%s",ss);
s = ss;
insert(i,s);
}
// cout<<start()<<endl;
printf("%d\n",start());
}
return 0;
}
分享到:
相关推荐
hdu 3341(ac自动机+状态压缩) 题意:容易理解... 思路:首先一开始容易想到要用到dp,开设一个dp[41][41][41][41][501]的数组来解决,但是明显内存已经超出范围了,于是就想如何减少内存呢?只要知道A、T、C、G其中...
hdu 1695 GCD(欧拉函数+容斥原理).docx
acm hdu as easy as a+b
HDU的一题........HDU DP动态规
【HDU 3993】田忌赛马 题解+勘误 题解这里就略写一下了,主要是勘误。 这道题是2011年之前的多校训练题,2020年的今天,我们一个集训队全部挂在上面了。最后在HDU看到了9年前的讨论区,才知道这题有如下问题: speed...
hdu2000-2014ac代码,虽然只有几道,但都是简单的
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
ACM题库,一些题目和答案,以及解题报告,传上来共享
hdu 一些简单题目 ac代码 大概100道
HDU最全ac代码
一个十分简单的程序,能够ac杭电hdu的第2050题,无注释,简单明了
杭电OnlineJudge 200-2099的解题报告
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,...
HDU上DP大集合,里面包括题,题解,代码,对DP入门者很实用,对DP老手也是有很大的提高
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
收集的部分HDOJ杭电ACM题的代码 大牛勿下 全是基础供初级acmer使用
动态规划DP题解 POJ HDU部分动态规划DP题解
hdu杭电所有题目按照ac数量排序,python分析
hdu2101AC代码
hdu 1005.比较简单的一道题,有兴趣的可以看看。