AC自动机+状态压缩dp+BFS求最短路+hdu3247

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
BestCoder官方QQ群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Resource Archiver

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1625    Accepted Submission(s): 488


Problem Description
Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.
Wait a minute… you realized that it isn’t as easy as you thought. Think about the virus killers. They’ll find your software suspicious, if your software contains one of the m predefined virus codes. You absolutely don’t want this to happen.
Technically, resource files and virus codes are merely 01 strings. You’ve already convinced yourself that none of the resource strings contain a virus code, but if you make the archive arbitrarily, virus codes can still be found somewhere.
Here comes your task (formally): design a 01 string that contains all your resources (their occurrences can overlap), but none of the virus codes. To make your software smaller in size, the string should be as short as possible.
 

Input
There will be at most 10 test cases, each begins with two integers in a single line: n and m (2 <= n <= 10, 1 <= m <= 1000). The next n lines contain the resources, one in each line. The next m lines contain the virus codes, one in each line. The resources and virus codes are all non-empty 01 strings without spaces inside. Each resource is at most 1000 characters long. The total length of all virus codes is at most 50000. The input ends with n = m = 0.
 

Output
For each test case, print the length of shortest string.
 

Sample Input
      
      
2 2 1110 0111 101 1001 0 0
 

Sample Output
      
      
5

题意:告诉n个串,然后问把这n个串拼接起来(重叠的算一次),并且不包含给出的m个字符串,问最短长度是多少

思路:首先处理出来每个字符串到另一个字符串的最短距离,dp[i][j],表示集合i(有哪些字符串已经包含)并且,当前在字符串j

转移方程:dp[i|val[ch[j][c]][k]=min(dp[i|val[ch[j][c]][k],dp[i][j]+g[j][c]);

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=60010;
const int SIGMA_SIZE=2;
const int INF=1000000000;
int n,m,cnt;
char s[1010];
int dis[maxn],pos[maxn],g[11][11],dp[1<<11][11];
struct AC
{
    int ch[maxn][2],val[maxn];
    int fail[maxn],last[maxn];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'0';}
    void insert(char *s ,int id)
    {
        int n=strlen(s);
        int u=0;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=id;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=last[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            if(val[fail[r]]==-1)val[r]=-1;
            else val[r]|=val[fail[r]];
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    void bfs(int u)
    {
        queue<int> q;
        q.push(pos[u]);
        memset(dis,-1,sizeof(dis));
        dis[pos[u]]=0;
        while(!q.empty())
        {
            int t=q.front();q.pop();
            for(int i=0;i<SIGMA_SIZE;i++)
            {
                int v=ch[t][i];
                if(dis[v]<0&&val[v]>=0)
                {
                    dis[v]=dis[t]+1;
                    q.push(v);
                }
            }
        }
        for(int i=0;i<cnt;i++)
            g[u][i]=dis[pos[i]];
    }
    void solve()
    {
        cnt=1;
        memset(g,0,sizeof(g));
        pos[0]=0;
        for(int i=0;i<sz;i++)
            if(val[i]>0)pos[cnt++]=i;
        for(int i=0;i<cnt;i++)
            bfs(i);
        for(int i=0;i<(1<<n);i++)
            for(int j=0;j<cnt;j++)
                dp[i][j]=INF;
        dp[0][0]=0;
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=0;j<cnt;j++)
            {
                if(dp[i][j]>=INF)continue;
                for(int c=0;c<cnt;c++)
                {
                    if(g[j][c]<0)continue;
                    if(j==c)continue;
                    int s=(i|val[pos[c]]);
                    dp[s][c]=min(dp[s][c],dp[i][j]+g[j][c]);
                }
            }
        }
        int ans=INF;
        for(int i=0;i<cnt;i++)
            ans=min(ans,dp[(1<<n)-1][i]);
        printf("%d\n",ans);
    }
}ac;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF,n+m)
    {
        ac.clear();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            ac.insert(s,1<<i);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            ac.insert(s,-1);
        }
        ac.getfail();
        ac.solve();
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值