HDU 3247 AC自动机 + 状态压缩dp
2013-01-18 13:10
302 查看
小HH:乳鸽的神题,状态很容易看出来,有50000*1024,很难保持,我用散列表超时了,用bitset刚好可以卡过,不过后来我想,只有尾结点才有效,中间的很多结点完全可以忽略,可以先用最短路吧各个尾结点之间的距离算出来,经过测试,不到50个点,马上就优化到50*1024了,本来9s多过的优化到了100多MS
#include <stdio.h> #include <math.h> #include <queue> #include <string.h> #include <algorithm> using namespace std; const int MAXN=60000;//结点的最大数,50000+1000*10 const int MAXL=(1<<10); const int MAX=2; const int MAXM=11; typedef struct Trie_Node { bool virus;//是否病毒 int end,fail; int next[MAX]; }Trie; Trie tree[MAXN]; char str[50010]; int dis[MAXN]; int pos[MAXM]; int G[MAXM][MAXM]; int dp[MAXL][MAXM]; int size,cnt; void insert(char *s,int id) { int p=0; int i=0; while(s[i]!='\0') { if(tree[p].next[s[i]-'0']==0) { tree[++size].end=0; tree[size].fail=0; tree[size].virus=false; for(int j=0;j<MAX;j++) tree[size].next[j]=0; tree[p].next[s[i]-'0']=size; } p=tree[p].next[s[i]-'0']; i++; } if(id>=0) tree[p].end=(1<<id); else tree[p].virus=true; } void BFS() //求出fail { queue<int>q; int temp,p; q.push(0); while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<MAX;i++) { if(tree[temp].next[i]!=0) { p=tree[temp].next[i]; q.push(p); if(temp!=0)tree[p].fail=tree[tree[temp].fail].next[i]; tree[p].end|=tree[tree[p].fail].end; tree[p].virus|=tree[tree[p].fail].virus; } else tree[temp].next[i]=tree[tree[temp].fail].next[i]; } } } void Path(int k) { queue<int>q; q.push(pos[k]); memset(dis,-1,sizeof(dis)); dis[pos[k]]=0; int now,p; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<MAX;i++) { p=tree[now].next[i]; if(dis[p]<0&&!tree[p].virus) { dis[p]=dis[now]+1; q.push(p); } } } for(int i=0;i<cnt;i++) G[k][i]=dis[pos[i]]; } inline int MIN(int x,int y) { if(x<0||y<0)return x>y?x:y; else return x>y?y:x; } void DoIt(int n) { memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int i=0;i<(1<<n);i++) for(int j=0;j<cnt;j++) { if(dp[i][j]<0)continue; for(int k=0;k<cnt;k++) { if(G[j][k]<0)continue; int t=i|tree[pos[k]].end; dp[t][k]=MIN(dp[t][k],dp[i][j]+G[j][k]); } } int t=(1<<n)-1; int ans=-1; for(int i=0;i<cnt;i++) ans=MIN(ans,dp[t][i]); printf("%d\n",ans); } int main() { int n,m; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; tree[0].end=0; tree[0].fail=0; tree[0].virus=false; for(int i=0;i<MAX;i++)tree[0].next[i]=0; size=0; for(int i=0;i<n;i++) { scanf("%s",&str); insert(str,i); } for(int i=0;i<m;i++) { scanf("%s",&str); insert(str,-1); } BFS(); cnt=1; pos[0]=0; for(int i=0;i<=size;i++) if(tree[i].end) pos[cnt++]=i; for(int i=0;i<cnt;i++) Path(i); DoIt(n); } return 0; }
相关文章推荐
- HDU 3247 Resource Archiver (AC自动机 + BFS + 状态压缩DP)
- HDU 3247 Resource Archiver(AC自动机+状态压缩dp+最短路BFS)
- HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )
- HDU 2825 Wireless Password (AC自动机 + 状态压缩DP)
- HDU 3247 Resource Archiver AC自动机 + bfs + 状态压缩dp
- HDU 3247 Resource Archiver(AC自动机+状态压缩DP)
- hdu 3247(ac自动机+状态压缩dp+最短路)
- hdu 2858 ac自动机 +状态压缩dp
- HDU 4758 Walk Through Squares( AC自动机 + 状态压缩DP )
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
- HDU 2825 Wireless Password(AC自动机 + 状态压缩DP 入门)
- HDU 3247 Resource Archiver(自动机+状态压缩DP)
- hdu 4317 Unfair Nim (状态压缩DP) 【2012 Multi-University Training Contest 2】
- hdu 1074(状态压缩dp)
- hdu 5135 状态压缩dp
- hdu 3681 二分+状态压缩dp+bfs
- Hdu 4057 Rescue the Rabbit (AC自动机+状态压缩dp) - 2011 ACM-ICPC Dalian Regional Contest Problem G
- HDU 4284(状态压缩dp)
- 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)
- HDU 4057 Rescue the Rabbit(AC自动机+状态压缩dp)