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.
InputThe 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.
You are to help the biologists to repair a DNA by changing least number of characters.
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. OutputFor 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 0Sample Output
Case 1: 1 Case 2: 4 Case 3: -1
题目大意:
对 给定的字符串进行修改,求修改至无病毒DNA的最小次数。
此题用DP来做,DP[i][j]代表修改至当前长度为i点数为j时的最小修改次数。
Status | Accepted |
---|---|
Time | 46ms |
Memory | 5540kB |
Length | 5040 |
Lang | G++ |
Submitted | 2017-04-13 00:40:55 |
Shared | |
RemoteRunId | 20397757 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 10007
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ul;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){
ll f=1;
while(q)
{
if(q&1) f=f*p%MOD;
p=p*p%MOD;
q>>=1;
}
return f;
}
map<char ,int >mp;
const int N = 1010;
const int M = 4;
class Trie
{
public:
int next[N][M],fail[N],end[N];
int root,L;
int newnode()
{
for(int i = 0;i < M;i++)//每一个节点对应0-128中的任意一个。
next[L][i] = -1;
end[L++] = -1;//表示下面没有节点 初始化,如果是记录次数,就赋0 还可以赋任意的数,
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char s[])
{
int len = strlen(s);
int now = root;
for(int i = 0;i < len;i++)
{
if(next[now][mp[s[i]]] == -1)
next[now][mp[s[i]]] = newnode();
now=next[now][mp[s[i]]];//记录其对应的节点编号
}
end[now]=1;//记录当前匹配单词的节点
//end[now]++;也可以用匹配单词结束后来记录次数
}
void build()
{
queue<int>Q;
fail[root] = root;//根节点仍然是根节点
for(int i = 0;i < M;i++)//对第一个字符遍历
if(next[root][i] == -1)//没有此字符开头
next[root][i] = root;//跳转到根
else//有此字符开头的
{
fail[next[root][i]] = root;//这个行位的失败指针为根
Q.push(next[root][i]);//行放入队列
}
while(!Q.empty())//还有字符
{
int now = Q.front();//逐层拿出第一个
Q.pop();
if(end[fail[now]]== 1) end[now] = 1;
for(int i = 0;i < M;i++)//对这一行
if(next[now][i] == -1)//如果下一行没有这个字符
next[now][i] = next[fail[now]][i];//他的下一个的这
else//如果有这个字符
{
fail[next[now][i]] = next[fail[now]][i];//他的下一个的
Q.push(next[now][i]);//下一行继续
}
}
}
/*bool used[N];//判断其是否被查找到
bool query(char buf[],int n,int id)
{
int len = strlen(buf);
int now = root;
memset(used,false,sizeof(used));//初始化used
bool flag = false;
for(int i = 0;i < len;i++)
{
now = next[now][buf[i]];
int temp = now;
while(temp != root)
{
if(end[temp] != -1)
{
used[end[temp]] = true;//记录被匹配的信息
flag = true;
}
temp = fail[temp];
}
}
}*/
int dp[1005][1005];
int solve(char buf[])
{
int len = strlen(buf);
for(int i=0;i<=len;i++)
{
for (int j = 0; j < L; ++j) {
dp[i][j] = inf_int;
}
}
dp[0][root] = 0;
for (int i = 0; i < len; ++i) {
for (int j = 0; j < L; ++j) {//代表走的步数
if(dp[i][j]!=inf_int)
{
for (int k = 0; k < 4; ++k) {
int news = next[j][k];
if(end[news]==1)continue;
int tmp;
if(k == mp[buf[i]])
{
tmp = dp[i][j];
}
else
tmp = dp[i][j]+1;
dp[i+1][news] = min(dp[i+1][news],tmp);
}
}
}
}
int ans = inf_int;
for (int l = 0; l < L; ++l) {
ans = min(ans,dp[len][l]);
}
if(ans==inf_int)
ans = -1;
return ans;
}
};
Trie ac;
char buf[1010];
int main()
{
mp['A'] = 0;
mp['C'] = 1;
mp['G'] = 2;
mp['T'] = 3;
int t = 1;
int n;
while(scanf("%d", &n) != EOF && n)
{
ac.init();
for(int i = 0; i < n; i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s", buf);
printf("Case %d: %d\n", t++, ac.solve(buf));
}
return 0;
}