![]() | ||||||||||
| ||||||||||
Resource ArchiverTime 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
Sample Output
|
题意:告诉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;
}