HDU 2457:DNA repair
2016-03-23 18:19
295 查看
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.
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
[b]dp乱跑[/b]
View Code
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
[b]dp乱跑[/b]
#include<iostream> #include<queue> #include<string> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int LO=4,NU=1005; inline int f(char u){ if (u=='A') return 0;else if (u=='C') return 1;else if (u=='G') return 2;else if (u=='T') return 3; } struct tree{ int f; bool w; int t[LO]; int v[LO]; }t[NU]; int n,m,p,num; bool us[NU]; char s[10000]; queue <int> q; int dp[1005][NU]; inline bool dfs(int x){ if (x==0) return 0; if (t[x].w) return 1; if (us[x]) return t[x].w; us[x]=1; return t[x].w|=dfs(t[x].f); } inline void in(){ int p=0,l,m=strlen(s); for (register int i=0;i<m;i++){ l=f(s[i]); if (!t[p].t[l]) t[p].t[l]=++num; p=t[p].t[l]; } t[p].w=1; } inline void mafa(){ register int i;int k,p; q.push(0);t[0].f=0; while(!q.empty()){ k=q.front();q.pop(); for (i=0;i<LO;i++) if (t[k].t[i]){ p=t[k].f; while((!t[p].t[i])&&p) p=t[p].f; t[t[k].t[i]].f=(k==p)?0:t[p].t[i]; q.push(t[k].t[i]); } } } const int INF=1e9; int main(){ int tt=0; register int i,j,k,l;int u;int ans; for(;;){ tt++; scanf("%d",&n); if (!n) return 0; num=u=ans=0; for (i=0;i<n;i++) scanf("%s",s),in(); mafa(); for (i=0;i<=num;i++) us[i]=0; for (i=0;i<=num;i++) t[i].w|=dfs(i); for (i=0;i<=num;i++) for (j=0;j<LO;j++){ if (!t[i].t[j]){ u=t[i].f; while(!t[u].t[j]&&u) u=t[u].f; u=t[u].t[j]; }else u=t[i].t[j]; t[i].v[j]=u; } scanf("%s",s); n=strlen(s); for (i=0;i<=n;i++) for (j=0;j<=num;j++) dp[i][j]=INF; dp[0][0]=0; for (i=0;i<n;i++) for (j=0;j<=num;j++) if (dp[i][j]!=INF) for (l=0;l<LO;l++) if (!t[t[j].v[l]].w) if (dp[i+1][t[j].v[l]]>dp[i][j]+(l==f(s[i])?0:1)) dp[i+1][t[j].v[l]]=dp[i][j]+(l==f(s[i])?0:1); ans=INF; for (j=0;j<=num;j++) if (dp [j]<ans) ans=dp [j]; if (ans==INF) ans=-1; printf("Case %d: %d\n",tt,ans); for (i=0;i<=num;i++) for (j=0;j<LO;j++) t[i].t[j]=t[i].v[j]=0; for (i=0;i<=num;i++) t[i].w=t[i].f=0; } }
View Code
相关文章推荐
- 杭电1787GCD Again(欧拉函数)
- [Leetcode 265]Paint House II
- [Leetcode 256]Paint House
- [AI]根据葡萄特征聚类
- [AI](加入随机因素)局部搜索解决N皇后问题(含源码)
- BN: Accelerating Deep Network Training by Reducing Internal Covariate Shift
- IBM openblockchain学习(二)--chaincode源码分析
- public static void main(String args[]){ }中的String args[ ]
- Convention 插件学习
- PAT 1014 Waiting in Line (30)
- 海外投行抢滩大数据 人工智能等领域趋热
- python的smtplib发送email给多人要传列表而不是','.join(LIST)
- QDialog QFrame QWidget QMainWindow区别
- wait与waitpid
- 【技术向】rainmeter的设计与发现
- shiro配置filterchaindefinitions实现多角色判定方法的重写
- Invalid file name: must contain only [a-z0-9_.]
- Groovy DSL (1) Command chains
- pair 函数 (STL)
- 记一次foxmailQQ邮箱创建失败到解决方法