zoukankan      html  css  js  c++  java
  • HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)

    题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长。

    析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1,如果访问知道就知道不可行的,然后处理出两两串叠加的最小长度,这个要用bfs,在AC自动机上把这个处理出来,然后剩下的就是一个简单的DP了,dp[s][i] 表示状态为 s 时,i 串在后面,长度最短是多少。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    //#define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    //#define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<LL, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int maxm = 1e6 + 5;
    const int mod = 10007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    const int maxnode = 6e4 + 100;
    const int sigma = 2;
    int dist[15][15], last[15], cnt;
    
    struct Aho{
      int ch[maxnode][sigma], f[maxnode];
      int val[maxnode];
      int sz;
    
      void clear(){ sz = 1;  ms(ch[0], 0); }
      inline int idx(char ch){ return ch - '0'; }
    
      void insert(char *s, int v){
        int u = 0;
        for(int i = 0; s[i]; ++i){
          int c = idx(s[i]);
          if(!ch[u][c]){
            ms(ch[sz], 0);
            val[sz] = 0;
            ch[u][c] = sz++;
          }
          u = ch[u][c];
        }
        if(v > 0)  val[u] |= 1<<v;
        else val[u] = v;
      }
    
      void getFail(){
        queue<int> q;
        f[0] = 0;
        for(int c = 0; c < sigma; ++c){
          int u = ch[0][c];
          if(u){ q.push(u);  f[u] = 0; }
        }
    
        while(!q.empty()){
          int r = q.front();  q.pop();
          for(int c = 0; c < sigma; ++c){
            int u = ch[r][c];
            if(!u){ ch[r][c] = ch[f[r]][c];  continue; }
            q.push(u);
            int v = f[r];
            while(v && !ch[v][c])  v = f[v];
            f[u] = ch[v][c];
            if(val[u] > 0 && val[f[u]] > 0)  val[u] |= val[f[u]];
          }
        }
      }
    
      int d[maxnode];
    
      void bfs(int s){
        queue<int> q;
        ms(d, INF);  d[last[s]] = 0;
        q.push(last[s]);
    
        while(!q.empty()){
          int u = q.front();  q.pop();
          for(int c = 0; c < sigma; ++c){
            int nxt = ch[u][c];
            if(d[nxt] > d[u] + 1 && val[nxt] >= 0){
              d[nxt] = d[u] + 1;
              q.push(nxt);
            }
          }
        }
        for(int i = 0; i < cnt; ++i)
          dist[s][i] = d[last[i]];
      }
    };
    
    Aho aho;
    
    char s[500006];
    int dp[2100][13];
    
    
    int main(){
      while(scanf("%d %d", &n, &m) == 2 && n+m){
        aho.cl;
        for(int i = 1; i <= n; ++i){
          scanf("%s", s);
          aho.insert(s, i);
        }
        for(int i = 0; i < m; ++i){
          scanf("%s", s);
          aho.insert(s, -1);
        }
        aho.getFail();
        cnt = 1;
        for(int i = 0; i < aho.sz; ++i)
          if(aho.val[i] > 0)  last[cnt++] = i;
        for(int i = 0; i < cnt; ++i)  aho.bfs(i);
        ms(dp, INF);
        dp[1][0] = 0;
        int all = 1<<cnt;
        FOR(i, 0, all)  for(int j = 0; j < cnt; ++j){
          if(dp[i][j] == INF)  continue;
          for(int k = 0; k < cnt; ++k){
            if(i&1<<k)  continue;
            dp[i|1<<k][k] = min(dp[i|1<<k][k], dp[i][j] + dist[j][k]);
          }
        }
        int ans = INF;
        for(int i = 0; i < cnt; ++i)
          ans = min(ans, dp[all-1][i]);
        printf("%d
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6
    CentOS 6.5下安装Python+Django+Nginx+uWSGI
    python学习之旅
    Gitlab安装操作说明书
    快速上手git gitlab协同合作
    在centos6.3用yum安装redis
    CentOS 6.5 下安装 Redis 2.8.7
    Ruby Gems更换淘宝源方法
    Apache 日志分析(一)
    Apache 日志分析(二)
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7689415.html
Copyright © 2011-2022 走看看