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.
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.
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.
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
题目大意:给出单词表和一个文本串,问最少修改几个字符使得该文本串不包含所有的单词。
解题思路:先根据单词表构建一个AC自动机,具体匹配的时候我们可以定义一个状态dp[i][j]表示长度为i、以字典树中j号结点结尾的字符串不包含所有单词所需的最少修改次数。很容易递推发现,dp[i+1][u]也就是长度为1+1、以当前结点结尾的字符串的最小修改次数等于 u的所有孩子结点ch[j][k]是否和当前s[i]相等的最小值。
找了一个小时的BUG,就是因为在把C字符转换成对应的数字时,判断力写的小写c,真的是太不细心了!
/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
typedef pair < int, int > P ;
const int Maxn = 60 * 30 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;
const int _Max = 1000 + 10 ;
int Next[Maxn][4], fail[Maxn], val[Maxn], cnt ;
int dp[Maxn][Maxn], n ;
string str, str1 ;
inline int change (char ch){
if (ch == 'A') return 0 ;
if (ch == 'C') return 1 ;
if (ch == 'G') return 2 ;
if (ch == 'T') return 3 ;
}
void Insert(){
int len = str.size() ;
int root = 0 ;
for (int i = 0; i < len; i++){
int id = change(str[i]) ;
if (!Next[root][id]) Next[root][id] = ++cnt ;
root = Next[root][id] ;
}
val[root] = 1 ;
}
void getFail (){
queue < int > que ;
for (int i = 0; i < 4; i++){
if (Next[0][i]) {
fail[Next[0][i]] = 0 ;
que.push(Next[0][i]) ;
}
}
while (!que.empty()){
int tmp = que.front() ;
que.pop() ;
if (val[fail[tmp]]) val[tmp] = 1 ;
for (int i = 0; i < 4; i++){
if (Next[tmp][i]){
fail[Next[tmp][i]] = Next[fail[tmp]][i] ;
que.push(Next[tmp][i]) ;
}
else Next[tmp][i] = Next[fail[tmp]][i] ;
}
}
}
void query(){
int len = str1.size();
for (int i = 0; i <= len; i++){
for (int j = 0; j < cnt; j++){
dp[i][j] = INF ;
}
}
dp[0][0] = 0 ;
for (int i = 0; i < len; i++){
for (int j = 0; j < cnt; j++){
if (dp[i][j] >= INF) continue ;
for (int k = 0; k < 4; k++){
int u = Next[j][k] ;
if (val[u]) continue ;
int tmp ;
if (k == change(str1[i])) tmp = dp[i][j] ;
else tmp = dp[i][j] + 1 ;
dp[i + 1][u] = min(dp[i + 1][u], tmp) ;
}
}
}
int ans = INF ;
for (int i = 0; i < cnt; i++) ans = min(dp[len][i], ans) ;
if (ans == INF) cout << -1 << endl ;
else cout << ans << endl ;
}
int main (){
int Cas = 0 ;
while (cin >> n && n){
cnt = 0 ;
memset(Next,0 ,sizeof(Next)) ;
memset(fail, 0, sizeof(fail)) ;
memset(val, 0, sizeof(val)) ;
for (int i = 1; i <= n; i++){
cin >> str ;
Insert() ;
}
getFail() ;
cin >> str1 ;
cout << "Case " << ++Cas << ": " ;
query() ;
}
return 0;
}