HDU 2457 DNA repair
2015-11-03 23:10
507 查看
DNA repair
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2457
64-bit integer IO format: %I64d Java class name: Main
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 thenumber 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 其实是Trie图
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1010; char str[maxn]; int dp[maxn][maxn],id[130],n,cs = 1; struct Trie { int ch[maxn][4],fail[maxn],tot; bool flag[maxn]; int newnode() { memset(ch[tot],0,sizeof ch[tot]); fail[tot] = flag[tot] = 0; return tot++; } void init() { tot = 0; newnode(); } void insert(char *str,int root = 0){ for(int i = 0; str[i]; ++i){ if(!ch[root][id[str[i]]]) ch[root][id[str[i]]] = newnode(); root = ch[root][id[str[i]]]; } flag[root] = true; } void build(int root = 0) { queue<int>q; for(int i = 0; i < 4; ++i) { if(ch[root][i]) { fail[ch[root][i]] = root; q.push(ch[root][i]); } } while(!q.empty()) { root = q.front(); q.pop(); for(int i = 0; i < 4; ++i) { int &nx = ch[root][i]; if(nx) { fail[nx] = ch[fail[root]][i]; flag[nx] = flag[nx]||flag[fail[nx]]; q.push(nx); } else nx = ch[fail[root]][i]; } } } void solve(char *str) { memset(dp,0x3f,sizeof dp); int len = strlen(str); dp[0][0] = 0; for(int i = 0; i < len; ++i) for(int j = 0; j < tot; ++j) { if(dp[i][j] >= INF) continue; for(int k = 0; k < 4; ++k) { int nx = ch[j][k]; if(flag[nx]) continue; dp[i+1][nx] = min(dp[i+1][nx],dp[i][j] + (id[str[i]] != k)); } } int ret = INF; for(int i = 0; i < tot; ++i) ret = min(ret,dp[len][i]); printf("Case %d: %d\n",cs++,ret == INF?-1:ret); } } ac; int main() { for(int i = 0; i < 4; ++i) id["ATCG"[i]] = i; while(scanf("%d",&n),n) { ac.init(); for(int i = 0; i < n; ++i) { scanf("%s",str); ac.insert(str); } ac.build(); scanf("%s",str); ac.solve(str); } return 0; }
View Code
相关文章推荐
- LightOJ - 1403 Air Raid(二分图)
- 【git/内源开发】fatal: Out of memory, malloc failed问题的解决
- [leetcode 172] Factorial Trailing Zeroes
- MOSS程序中如何发Mail?
- 2015-11-3-Training(for 2014th)
- 如何使用sendEmail发送邮件
- LeetCode——Contains Duplicate III
- Android SDK安装更新:Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml
- 【leetcode】11. Container With Most Water
- leetcode-Contains Duplicate
- error C2556: “void main(void)”: 重载函数与“int main(void)”只是在返回类型上不同
- LINK : error LNK2001: 无法解析的外部符号 _mainCRTStartup
- Repair the Wall
- http://blog.csdn.net/chenli522/article/details/17001823
- Daily Scrum (2015/11/2)
- Hdu 2476 String painter (区间DP)
- 创建新项目paimai的一些记录
- 解决gitosis.init.InsecureSSHKeyUsername: Username contains not allowed characters问题
- 【UKIEPC2015 J】【趣味迷宫搜索】Jelly Raid 来回巡逻 不被发觉 循环节预处理
- No source available for "0x0" 出现这种问题的处理方法