HDU3247 Resource Archiver

Resource Archiver

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



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
 

Source
 
题意:给n个01串和m个病毒串,要求用01串拼出最短的不包含病毒串的串,输出最短的长度。
分析:先建立AC自动机,插入的时候每个串的结尾标记都用二进制表示出来,病毒串也要单独标记出来,这样在建立自动机的时候,可以运用或运算,把fail指针指向的那一个节点的end也加入到这个节点里来,病毒也同样,然后要对每个有末尾标记的节点广搜下,求出该串与其他串的最短距离,最后就是状态压缩DP了,因为我们插入的时候end标记用了二进制表示,所以在状态压缩的时候也方便,枚举状态和每个有标记的节点,求出每个状态对应的节点的最小值(dp[i][j],i表示状态,j表示目前所在的节点,这里的状态就是使用了哪一个01串),最后枚举状态(1<<n)-1时的所有节点找出里面的最小值,因为我们要求要使用完所有的01串。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=60010;
const int MAXM=200;
char s[1010];
int pos[MAXM],G[MAXM][MAXM],d[MAXN];
int dp[1<<11][MAXM];
int size,cnt;
struct node
{
    int fail,next[2],end;
    bool virus;
    void init()
    {
        memset(next,0,sizeof(next));
        fail=end=0;
        virus=0;
    }
}tree[MAXN];
void insert(char *str,int flag)
{
    int i=0,p=0,index;
    while(str[i])
    {
        index=str[i]-'0';
        if(!tree[p].next[index])
        {
            tree[++size].init();
            tree[p].next[index]=size;
        }
        p=tree[p].next[index];
        i++;
    }
    if(flag>=0)
        tree[p].end=1<<flag;
    else
        tree[p].virus=1;
}
void build_ac_automation()
{
    int i;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(i=0;i<2;i++)
        {
            if(tree[temp].next[i])
            {
                int p=tree[temp].next[i];
                if(temp)
                    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;
                q.push(p);
            }
            else
                tree[temp].next[i]=tree[tree[temp].fail].next[i];
        }
    }
}
void path(int k)
{
    int i;
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(pos[k]);
    d[pos[k]]=0;
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(i=0;i<2;i++)
        {
            int p=tree[temp].next[i];
            if(d[p]<0&&!tree[p].virus)
            {
                d[p]=d[temp]+1;
                q.push(p);
            }
        }
    }
    for(i=0;i<cnt;i++)
        G[k][i]=d[pos[i]];
}
int MIN(int x,int y)
{
    if(x<0||y<0)
        return x>y?x:y;
    return x>y?y:x;
}
void solve(int n)
{
    int i,j,k,t,ans;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for(i=0;i<(1<<n);i++)
    {
        for(j=0;j<cnt;j++)
        {
            if(dp[i][j]<0)
                continue;
            for(k=0;k<cnt;k++)
            {
                if(G[j][k]<0)
                    continue;
                t=i|tree[pos[k]].end;
                dp[t][k]=MIN(dp[t][k],dp[i][j]+G[j][k]);
            }
        }
    }
    t=(1<<n)-1;
    ans=-1;
    for(i=0;i<cnt;i++)
        ans=MIN(ans,dp[t][i]);
    printf("%d\n",ans);
}
int main()
{
    int n,m,i,j;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)
            break;
        size=0;
        tree[0].init();
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            insert(s,i);
        }
        while(m--)
        {
            scanf("%s",s);
            insert(s,-1);
        }
        build_ac_automation();
        cnt=1;
        pos[0]=0;
        for(i=0;i<=size;i++)
            if(tree[i].end)
                pos[cnt++]=i;
        for(i=0;i<cnt;i++)
            path(i);
        solve(n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值