HDU3247 Resource Archiver (AC自动机+spfa+状压DP)

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.

InputThere 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.OutputFor each test case, print the length of shortest string.Sample Input

2 2
1110
0111
101
1001
0 0

Sample Output

5

题解:AC自动机的fail指针跑spfa状压DP求最短串

总体思路是利用代码串和病毒串建立自动机,他们在自动机上的差别是一个末节点标记,一个不标记。然后将每个代码串尾节点看做图上的一个节点,利用自动机计算每个串其他所有串的不重叠的最短长度即两两节点间的最短距离。最后转变成TSP问题,状态压缩DP解之。

    第一眼看到那个n,小等于10,soga,状态压缩,稍微思考下就能将问题转换成这样一个模型:n个串必须都选且选一次,求这n个串的排列使得组合成的串不包含病毒串并且长度最小。啊哈,这不是TSP问题吗?是的,你没有看错,转换成了TSP问题。

   转换成TSP问题之后,我们想的是怎么让长度尽量小,考虑将两个代码串重叠起来。两个代码串a,b的前缀和后缀可能相等,他们组成的最短不包含病毒串的字符串c,前面部分为a,后面部分为b,这时候再来个d代码串要和前面两个合体,那么就成c和d的重叠问题了。

   接下来我们要做的怎么让代码串a和代码串b组成的串c长度最小且不包含病毒串呢?我一开始用kmp来找两个串的相等前缀、后缀,然后组成串去ac自动机中匹配。然后一瞬间我就觉得我自己脑残了,这不是让ac自动机退化成kmp和字典树了吗!因为ac自动机上的一个节点到根的路径代表一个字符串,假设串a的末尾节是p,b的末尾节点是q,接着我们要做是在p点利用next数组转移到q,我们得到一个结论:从p到q所走的路径便是b除开与a重叠部分的那个后缀,如a为aaabb,b为bbaaa,那么路经就代表串b的aaa子串。我们怎么保证从p点走到q点,中间走过的路径表示的串一定是串b的后缀呢?两种情况:1、a是b的子串,这时候我们不会用到fail指针,显然可以 2、我们需要用到fail指针,每次用fail指针找到下一个匹配的位置假设是failx,failx节点到根节点所表示的串便是我们走过路径的最长后缀,这样一直找找到节点q,点q到根节点所表示的串遍是我们走过路径的最长后缀,然后上面的结论便得证。

    总而言之,我们在ac自动机上走过的路径可以表示一个串,设为S,到达点p,那么点p到根节点这条路径所表示的串s,s为S的后缀。为用路径代表一个串是ac自动机优美之处。

    我们从上面说的p点走到q点会有很多路径,要保证走过的路径长度最小即b串于a串的不重叠部分最短,要用到spfa,其实本题就退化成普通的Bfs,因为没有松弛操作。这样得到就可以得到各串相互之间的最短距离,然后就变成了很普通的TSP。

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1.0)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1010;
const int maxnode=60010;
char s[maxn];
int n,m,tot,ans,ln[20];
int pre[maxn],vis[maxnode],dis[20][maxnode],dp[(1<<10)][15];
int ch[maxnode][2],link[maxnode],fail[maxnode],val[maxnode];

void Init()
{ 
    tot=1; ans=INF; 
    memset(ch[0],0,sizeof(ch[0]));
}

int GetId(char ch){return ch-'0';}

void Insert(int tk)
{
    scanf("%s",s);
    int len=strlen(s),u=0;
    if(tk!=-1) ln[tk]=len;
    for(int i=0;i<len;++i)
    {
        int x=GetId(s[i]);
        if(!ch[u][x])
        {
            memset(ch[tot],0,sizeof(ch[tot]));
            val[tot]=0;
            ch[u][x]=tot++;
        }
        u=ch[u][x];
    }
    if(tk==-1) val[u]=-1;
    else val[u]=tk,pre[tk]=u;
}

void GetFail()
{
    queue<int> q; 
    q.push(0); fail[0]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=0;i<=1;++i)
        {
            if(ch[u][i])
            {
                fail[ch[u][i]] = u?ch[fail[u]][i]:0;
                q.push(ch[u][i]);
            }
            else ch[u][i]=ch[fail[u]][i];
        }
    }
}

void spfa(int s)
{
    memset(dis[s],INF,sizeof(dis[s]));
    memset(vis,0,sizeof(vis));
    dis[s][pre[s]]=0;
    queue<int> q;
    q.push(pre[s]);vis[pre[s]]=1;
    while(!q.empty())
    {
        int u=q.front();
        for(int i=0;i<=1;++i)
        {
            if(val[ch[u][i]]!=-1)
            {
                if(dis[s][ch[u][i]]>dis[s][u]+1)
                {
                    dis[s][ch[u][i]]=dis[s][u]+1;
                    if(!vis[ch[u][i]])
                    {
                        vis[ch[u][i]]=1;
                        q.push(ch[u][i]);
                    }
                }
            }
        } 
        q.pop(); vis[u]=0;
    }
}

bool check(int x,int y,int z)
{
    if(((1<<y-1)&z)==0) return false;
    int num=0;
    for(int i=1;i<=n;++i){ if((1<<i-1)&z) num++; }
    return num==x? true:false;
}

void work()
{
    memset(dp,INF,sizeof(dp));
    for(int i=1;i<=n;++i) dp[(1<<i-1)][i]=ln[i];
    for(int i=1;i<n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            for(int k=1;k<=(1<<n)-1;++k)
            {
                if(check(i,j,k))
                {
                    for(int l=1;l<=n;++l)
                    {
                        if((k&(1<<l-1)) == 0) 
                            dp[k+(1<<l-1)][l]=min(dp[k+(1<<l-1)][l],dp[k][j]+dis[j][pre[l]]);
                    }
                }
            } 
        }
    }
    for(int i=1;i<=n;++i) ans=min(ans,dp[(1<<n)-1][i]);
}

int main()
{
    while(scanf("%d%d",&n,&m)&&n&&m)
    {
        Init();
        for(int i=1;i<=n;++i) Insert(i);
        for(int i=1;i<=m;++i) Insert(-1);
        GetFail();
        for(int i=1;i<=n;++i) spfa(i);
        work();
        printf("%d\n",ans);    
    }
    return 0;
}
View Code

 

posted @ 2019-05-08 16:49  StarHai  阅读(300)  评论(0编辑  收藏  举报