【ZOJ】 3190 Resource Archiver AC自动机+BFS预处理+DP

Resource Archiver

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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

Author: LIU, Rujia

Source: The 34th ACM-ICPC Asia Regional 2009 Ningbo Site NIT Cup National Invitation Contest


传送门:【ZOJ】 3190 Resource Archiver


题目大意:给你n个资源串,m个病毒串,你的目的是构造一个尽量短的串使得包含所有的资源串并且不包含任意一个病毒串。 (2 <= n <= 10, 1 <= m <= 1000),所有串都是非空的01序列,且每个资源串长度不大于1000,病毒串总长小于50000。


题目分析:

真是伤。。过了好久才想出这题该怎么写。TUT

一开始先将资源串和病毒串都插入到AC自动机中。

分别记录病毒串的结尾和资源串的结尾。

然后在自动机上跑BFS,得到从一个资源串 j 的结尾到另外其他资源串 k 的结尾的最短移动步长dist[ j ][ k ]。

然后以dp[ i ][ j ]表示在状态 i 下以资源串 j 为结尾的合法串的最短长度。

则dp[ i | ( 1 << k )][ k ] = min ( dp[ i | ( 1 << k )][ k ] , dp[ i ][ j ] + dist[ j ][ k ] )。

然后就解出来了= =||真是伤。

PS:HDU数据满弱的样子,然后又去ZOJ交了,不知道ZOJ数据够不够强,希望自己写的没什么漏洞


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define clear( a , x ) memset ( a , x , sizeof a )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define CHAR( i ) for ( int i = 0 ; buf[i] ; ++ i )

const int MAXN = 60005 ;
const int MAXW = 2 ;
const int MAXQ = 1000000 ;
const int INF = 0x3f3f3f3f ;
const int NUM = 10 ;

struct Trie {
	int next[MAXN][MAXW] ;
	int fail[MAXN] ;
	int word[MAXN] ;
	int virus[MAXN] ;
	int Q[MAXQ] ;
	int head , tail ;
	int P , root ;
	int d[MAXN] ;
	int dist[NUM][NUM] ;
	bool vis[MAXN] ;
	int len[NUM] ;
	int dp[1 << NUM][NUM] ;
	
	int newnode () {
		REP ( i , MAXW )
			next[P][i] = -1 ;
		word[P] = 0 ;
		virus[P] = 0 ;
		return P ++ ;
	}
	
	void init () {
		P = 0 ;
		clear ( dist , INF ) ;
		clear ( len , 0 ) ;
		root = newnode () ;
	}
	
	int get ( char c ) {
		return c - '0' ;
	}
	
	int insert ( char buf[] , int idx ) {
		int now = root ;
		CHAR ( i ) {
			int x = get ( buf[i] ) ;
			if ( next[now][x] == -1 )
				next[now][x] = newnode () ;
			now = next[now][x] ;
			++ len[idx] ;
		}
		if ( idx == -1 )
			virus[now] = 1 ;
		else {
			word[now] = ( 1 << idx ) ;
			return now ;
		}
	}
	
	void build () {
		head = tail = 0 ;
		fail[root] = root ;
		REP ( i , MAXW ) {
			if ( next[root][i] == -1 )
				next[root][i] = root ;
			else {
				fail[next[root][i]] = root ;
				Q[tail ++] = next[root][i] ;
			}
		}
		while ( head != tail ) {
			int now = Q[head ++] ;
			REP ( i , MAXW ) {
				if ( next[now][i] == -1 )
					next[now][i] = next[fail[now]][i] ;
				else {
					fail[next[now][i]] = next[fail[now]][i] ;
					word[next[now][i]] |= word[fail[next[now][i]]] ;
					virus[next[now][i]] |= virus[fail[next[now][i]]] ;
					Q[tail ++] = next[now][i] ;
				}
			}
		}
	}
	
	void bfs ( int x , int idx , int n ) {
		clear ( vis , 0 ) ;
		head = tail = 0 ;
		Q[tail ++] = x ;
		d[x] = 0 ;
		vis[x] = 1 ;
		if ( word[x] )
			REP ( i , n )
				if ( word[x] & ( 1 << i ) )
					dist[idx][i] = 0 ;
		while ( head != tail ) {
			int now = Q[head ++] ;
			REP ( i , MAXW ) {
				int nxt = next[now][i] ;
				if ( !virus[nxt] && !vis[nxt] ) {
					vis[nxt] = 1 ;
					d[nxt] = d[now] + 1 ;
					Q[tail ++] = nxt ;
					if ( word[nxt] )
						REP ( j , n )
							if ( word[nxt] & ( 1 << j ) )
								if ( dist[idx][j] > d[nxt] )
									dist[idx][j] = d[nxt] ;
				}
			}
		}
	}
	
	void solve ( int n ) {
		clear ( dp , INF ) ;
		int o = 1 << n ;
		REP ( i , n )
			dp[1 << i][i] = len[i] ;
		REP ( i , o )
			REP ( j , n )
				REP ( k , n )
					dp[i | ( 1 << k )][k] = min ( dp[i | ( 1 << k )][k] , dp[i][j] + dist[j][k] ) ;
		int ans = INF ;
		REP ( i , n )
			if ( ans > dp[o - 1][i] )
				ans = dp[o - 1][i] ;
		printf ( "%d\n" , ans ) ;
	}
} ;

Trie ac ;
char buf[MAXN] ;
int idx[NUM] ;

void work () {
	int n , m ;
	while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) {
		ac.init () ;
		REP ( i , n ) {
			scanf ( "%s" , buf ) ;
			idx[i] = ac.insert ( buf , i ) ;
		}
		REP ( i , m ) {
			scanf ( "%s" , buf ) ;
			ac.insert ( buf , -1 ) ;
		}
		ac.build () ;
		REP ( i , n )
			ac.bfs ( idx[i] , i , n ) ;
		ac.solve ( n ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值