DNA repair
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1537 Accepted Submission(s): 826
Problem Description
Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters 'A', 'G' , 'C' and 'T'. The repairing techniques are simply to change some characters to eliminate all segments causing diseases. For example, we can repair a DNA "AAGCAG" to "AGGCAC" to eliminate the initial causing disease segments "AAG", "AGC" and "CAG" by changing two characters. Note that the repaired DNA can still contain only characters 'A', 'G', 'C' and 'T'.
You are to help the biologists to repair a DNA by changing least number of characters.
You are to help the biologists to repair a DNA by changing least number of characters.
Input
The input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases.
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "AGCT", which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "AGCT", which is the DNA to be repaired.
The last test case is followed by a line containing one zeros.
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "AGCT", which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "AGCT", which is the DNA to be repaired.
The last test case is followed by a line containing one zeros.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by the
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
Sample Input
2 AAA AAG AAAG 2 A TG TGAATG 4 A G C T AGT 0
Sample Output
Case 1: 1 Case 2: 4 Case 3: -1
Source
2008 Asia Hefei Regional Contest Online by USTC
题目大意:修改原串最少的字符构造出不患病的基因串
题目分析:ac自动机可以求取匹配,然后用dp[i][j],i表示串的当前位置,j表示自动机的状态,枚举四种情况,当当前枚举字符与原串相同,直接转移,否则+1后转移
注意字符串是从0开始的,读入时要读到s+1里,坑死我了,wa得肉疼
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#define N 1007
#define kind 4
using namespace std;
char s[N];
int Next[N*100][kind],fail[N*100],pos;
bool flag[N*100];
int index ( char ch )
{
if ( ch == 'A' ) return 0;
if ( ch == 'C' ) return 1;
if ( ch == 'G' ) return 2;
if ( ch == 'T' ) return 3;
}
int newNode ( )
{
for ( int i = 0 ; i < kind ; i++ )
Next[pos][i] = 0;
fail[pos] = flag[pos] = 0;
return pos++;
}
void insert ( char *s )
{
int i , p = 0;
for ( i = 0 ; s[i] ; i ++ )
{
int k = index ( s[i] ) , &x = Next[p][k];
p = x?x : x = newNode ( );
}
flag[p] = true;
}
void build ( )
{
int i;
queue<int> q;
q.push ( 0 );
while ( !q.empty ( ) )
{
int u = q.front();
//cnt[u] += cnt[fail[u]];
q.pop ( );
/*for ( int i = 0 ; i < kind ; i++ )
{
int v = Next[u][i];
if ( v == 0 ) Next[u][i] = Next[fail[u]][i];
else q.push(v);
if ( u && v )
fail[v] = Next[fail[u]][i];
}*/
for ( int i = 0 ; i < 4 ; i++ )
{
int v = Next[u][i];
if ( v )
{
q.push ( v );
if ( u )
fail[v] = Next[fail[u]][i];
flag[v] |= flag[fail[v]];
}
else
Next[u][i] = Next[fail[u]][i];
}
}
}
int dp[N][N*2];
int INF;
void solve ( char *s , int n )
{
memset ( dp , 0x3f , sizeof ( dp ) );
INF = dp[0][0];
dp[0][0] = 0;
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 0 ; j < pos ; j++ )
{
if ( dp[i-1][j] >= INF || flag[j] ) continue;
for ( int k = 0 ; k < kind ; k++ )
{
int p = Next[j][k];
if ( flag[p] ) continue;
if ( index(s[i]) == k )
dp[i][p] = min ( dp[i][p] , dp[i-1][j] );
else
dp[i][p] = min ( dp[i][p] , dp[i-1][j]+1 );
}
}
}
int n;
int main ( )
{
int c = 1;
while ( ~scanf ( "%d" , &n ),n )
{
pos = 0 , newNode ( );
while ( n-- )
{
scanf ( "%s" ,s );
insert ( s );
}
build ( );
scanf ( "%s" , s+1 );
int len = strlen (s+1);
solve ( s , len );
int ans = INF;
for ( int i = 0 ; i < pos ; i++ )
ans = min ( ans , dp[len][i] );
printf ( "Case %d: %d\n" , c++ , ans>=100007?-1:ans );
}
}