HDU 2457/POJ 3691 DNA repair AC自动机+DP
2017-03-21 20:09
375 查看
DNA repair
[align=left][b]Problem Description[/b][/align][align=left] [/align]
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.
[align=left][b]Input[/b][/align]
[align=left] [/align]
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.
[align=left][b]Output[/b][/align]
[align=left] [/align]
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.
[align=left][b]Sample Input[/b][/align]
2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0
[align=left][b]Sample Output[/b][/align]
Case 1: 1
Case 2: 4
Case 3: -1
[b]题意:[/b]
给你n个模式串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含任何一个模式串
[b]题解:[/b]
将这个n个模式串建立AC自动机
设定dp[i][j]表示在trie树上第j个节点的状态下能让前i个字符保持不包含任何一个模式串的最小修改量
简单转移
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define pii pair<int,int> #define MP make_pair typedef long long LL; const long long INF = 1e18+1LL; const double Pi = acos(-1.0); const int N = 5e5+10, M = 1e3+20, mod = 1e9+7, inf = 1e9; int dp[1100][2100],sum ,nex [5],cnt,head,tail,q ,fail ; int ID(char x) { if(x == 'A') return 1; else if(x == 'T') return 2; else if(x == 'C') return 3; else return 0; } void insert(char *s) { int now = 1,len = strlen(s); for(int i = 0; i < len; ++i) { int index = ID(s[i]); if(!nex[now][index]) nex[now][index] = ++cnt; //sum[nex[now][index]] |= sum[now]; now = nex[now][index]; } sum[now] = 1; } void build_fail() { head = 0, tail = 0; for(int i = 0; i < 4; ++i) nex[0][i] = 1; fail[1] = 0; q[tail++] = 1; while(head!=tail) { int now = q[head++]; sum[now] |= sum[fail[now]]; for(int i = 0; i < 4; ++i) { int p = fail[now]; if(!nex[now][i]) { nex[now][i] = nex[p][i];continue; } fail[nex[now][i]] = nex[p][i]; q[tail++] = nex[now][i]; } } } int n; char s ,a ; int main() { int cas = 1; while(scanf("%d",&n)!=EOF) { if(n == 0) break; cnt = 1; memset(sum,0,sizeof(sum)); memset(nex,0,sizeof(nex)); memset(fail,0,sizeof(fail)); for(int i = 1; i <= n; ++i) { scanf("%s",s); insert(s); } build_fail(); scanf("%s",a+1); n = strlen(a+1); for(int i = 0; i <= n; ++i) for(int j = 1; j <= cnt; ++j) dp[i][j] = inf; dp[0][1] = 0; for(int i = 0; i < n; ++i) { for(int j = 1; j <= cnt; ++j) { if(dp[i][j] == inf) continue; for(int k = 0; k < 4; ++k) { if(k == ID(a[i+1])) { if(!sum[nex[j][k]])dp[i+1][nex[j][k]] = min(dp[i+1][nex[j][k]],dp[i][j]); } else { if(!sum[nex[j][k]]) dp[i+1][nex[j][k]] = min(dp[i+1][nex[j][k]],dp[i][j]+1); } } } } int ans = inf; for(int i = 1; i <= cnt; ++i) { ans = min(dp [i],ans); } printf("Case %d: ",cas++); if(ans == inf) puts("-1"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 3691 & HDU 2457 DNA repair (AC自动机,DP)
- 【HDU 2457】 【POJ 3691】 DNA repair AC自动机+DP;
- Poj 3691 & Hdu 2457 DNA repair (AC自动机+DP)
- 【POJ 3691】【hdu 2457】DNA repair 中文题意&题解&代码(C++)
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
- HDU 2457 || POJ 3691 DNA repair (AC自动机 + dp)
- Poj 3691 & Hdu 2457 DNA repair
- Stoer-Wagner算法训练题目合集 hdu 3691 hdu3002 poj 2914
- hdu 1671 & poj 3630 Phone List
- poj 2182 || hdu 2711 Lost Cows
- poj 2186 && hdu 3836
- Poj 3819 // Hdu 3425 Coverage
- HDU_1055 && POJ_2054 Color a Tree(贪心)
- HDU 1671 & POJ 3630 Phone List
- HDU 1147 poj 2653 Pick-up sticks
- poj 3691 DNA repair 最少修改几处,使S不含任何模板DNA(可重叠)
- poj 1151 && hdu 1542 求矩形面积并(线段树)
- 划分树学习(poj 2104,hdu 3473)
- HDU 3691 Nubulsa Expo
- hdu 1700 poj 2954