HDU 2457 || POJ 3691 DNA repair (AC自动机 + dp)
2017-08-19 09:44
411 查看
题意:
给了你n 个病毒DNA串, 和一个模板串, 要求模板串上改尽量少的字符 使得 模板串不包含 病毒串。
思路:
AC自动机 + dp
多个串匹配, 很容易想到是AC自动机, 然后又是改少的字符, 也比较容易想到 dp 上。
令dp[i][j] 表示 使模板串前i 个字符 在自动机的j 节点,不包含病毒串 的最小修改数量。
那么我们直接枚举第i-1 个字符所在的节点, 然后向下走一位。 如果下一位 和 原串 不同就加一, 否则不变。
注意:
如果一个串的子串 是病毒串的话,那么原串也是病毒, 重新跑一边fail 指针就可以解决这个问题了。
例如这个样例:
2
ATGCAT
GC
ATGCAC
应输出1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n;
int get(char ch){
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'G') return 2;
if (ch == 'T') return 3;
}
const int maxn = 1000000 + 10;
const int inf = 1000000;
int ks;
struct Trie{
int root, L;
int flag[maxn];
int next[maxn][4];
int fail[maxn];
int dp[1007][3000];
void init(){
L = 0;
root = newnode();
}
int newnode(){
for (int i = 0; i < 4; ++i){
next[L][i] = -1;
}
flag[L] = 0;
return L++;
}
void bfs(){
fail[root] = root;
queue<int>q;
for (int i = 0; i < 4; ++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 u = q.front(); q.pop();
for (int i = 0; i < 4; ++i){
if (next[u][i] == -1){
next[u][i] = next[fail[u] ][i];
}
else {
fail[next[u][i] ] = next[fail[u] ][i];
q.push(next[u][i]);
}
}
}
}
void insert(char* s){
int len = strlen(s);
int nod = root;
for (int i = 0; i < len; ++i){
int id = get(s[i]);
if (next[nod][id] == -1){
next[nod][id] = newnode();
}
nod = next[nod][id];
}
flag[nod] = 1;
}
void query(char* s){
int len = strlen(s);
int nod = root;
for (int i = 0; i < L; ++i){
int tmp = i;
while(tmp != root){
if (flag[tmp]) {
flag[i] = 1;
break;
}
tmp = fail[tmp];
}
}
for (int i = 0; i <= len; ++i){
for (int j = 0; j < L; ++j){
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for (int i = 1; i <= len; ++i){
char ch = s[i-1];
int id = get(ch);
for (int j = 0; j < L; ++j){
if (dp[i-1][j] >= inf || flag[j]) continue;
for (int k = 0; k < 4; ++k){
int nx = next[j][k];
if (flag[nx]) continue;
int id = get(ch);
if (id == k){
dp[i][nx] = min(dp[i][nx], dp[i-1][j]);
}
else {
dp[i][nx] = min(dp[i][nx], dp[i-1][j] + 1);
}
}
}
}
int ans = inf;
for (int i = 0; i < L; ++i){
ans = min(ans, dp[len][i]);
}
printf("Case %d: ", ++ks);
if (ans >= inf) puts("-1");
else printf("%d\n", ans);
}
}ac;
char word[55];
char s[1007];
int main(){
while(~scanf("%d", &n)){
if (n == 0) break;
ac.init();
for (int i = 0; i < n; ++i){
scanf("%s", word);
ac.insert(word);
}
ac.bfs();
scanf("%s", s);
ac.query(s);
}
return 0;
}
/**
2
ATGCAT
GC
ATGCAC
**/
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2693 Accepted Submission(s): 1443
Problem 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
Source
2008 Asia Hefei Regional Contest Online by USTC
Recommend
teddy
Statistic | Submit | Discuss | Note
给了你n 个病毒DNA串, 和一个模板串, 要求模板串上改尽量少的字符 使得 模板串不包含 病毒串。
思路:
AC自动机 + dp
多个串匹配, 很容易想到是AC自动机, 然后又是改少的字符, 也比较容易想到 dp 上。
令dp[i][j] 表示 使模板串前i 个字符 在自动机的j 节点,不包含病毒串 的最小修改数量。
那么我们直接枚举第i-1 个字符所在的节点, 然后向下走一位。 如果下一位 和 原串 不同就加一, 否则不变。
注意:
如果一个串的子串 是病毒串的话,那么原串也是病毒, 重新跑一边fail 指针就可以解决这个问题了。
例如这个样例:
2
ATGCAT
GC
ATGCAC
应输出1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n;
int get(char ch){
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'G') return 2;
if (ch == 'T') return 3;
}
const int maxn = 1000000 + 10;
const int inf = 1000000;
int ks;
struct Trie{
int root, L;
int flag[maxn];
int next[maxn][4];
int fail[maxn];
int dp[1007][3000];
void init(){
L = 0;
root = newnode();
}
int newnode(){
for (int i = 0; i < 4; ++i){
next[L][i] = -1;
}
flag[L] = 0;
return L++;
}
void bfs(){
fail[root] = root;
queue<int>q;
for (int i = 0; i < 4; ++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 u = q.front(); q.pop();
for (int i = 0; i < 4; ++i){
if (next[u][i] == -1){
next[u][i] = next[fail[u] ][i];
}
else {
fail[next[u][i] ] = next[fail[u] ][i];
q.push(next[u][i]);
}
}
}
}
void insert(char* s){
int len = strlen(s);
int nod = root;
for (int i = 0; i < len; ++i){
int id = get(s[i]);
if (next[nod][id] == -1){
next[nod][id] = newnode();
}
nod = next[nod][id];
}
flag[nod] = 1;
}
void query(char* s){
int len = strlen(s);
int nod = root;
for (int i = 0; i < L; ++i){
int tmp = i;
while(tmp != root){
if (flag[tmp]) {
flag[i] = 1;
break;
}
tmp = fail[tmp];
}
}
for (int i = 0; i <= len; ++i){
for (int j = 0; j < L; ++j){
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for (int i = 1; i <= len; ++i){
char ch = s[i-1];
int id = get(ch);
for (int j = 0; j < L; ++j){
if (dp[i-1][j] >= inf || flag[j]) continue;
for (int k = 0; k < 4; ++k){
int nx = next[j][k];
if (flag[nx]) continue;
int id = get(ch);
if (id == k){
dp[i][nx] = min(dp[i][nx], dp[i-1][j]);
}
else {
dp[i][nx] = min(dp[i][nx], dp[i-1][j] + 1);
}
}
}
}
int ans = inf;
for (int i = 0; i < L; ++i){
ans = min(ans, dp[len][i]);
}
printf("Case %d: ", ++ks);
if (ans >= inf) puts("-1");
else printf("%d\n", ans);
}
}ac;
char word[55];
char s[1007];
int main(){
while(~scanf("%d", &n)){
if (n == 0) break;
ac.init();
for (int i = 0; i < n; ++i){
scanf("%s", word);
ac.insert(word);
}
ac.bfs();
scanf("%s", s);
ac.query(s);
}
return 0;
}
/**
2
ATGCAT
GC
ATGCAC
**/
DNA repair
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2693 Accepted Submission(s): 1443
Problem 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
Source
2008 Asia Hefei Regional Contest Online by USTC
Recommend
teddy
Statistic | Submit | Discuss | Note
相关文章推荐
- POJ 3691 & HDU 2457 DNA repair (AC自动机,DP)
- Poj 3691 & Hdu 2457 DNA repair (AC自动机+DP)
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
- POJ 3691 DNA repair 基于AC自动机的DP
- 【POJ 3691】【hdu 2457】DNA repair 中文题意&题解&代码(C++)
- Poj 3691 & Hdu 2457 DNA repair
- HDU 2457 DNA repair (AC自动机 + DP)
- POJ 3691 DNA repair (AC自动机 + dp)
- poj 3691 DNA repair 【ac自动机 + dp + spfa】
- 【HDU 2457】 【POJ 3691】 DNA repair AC自动机+DP;
- POJ 3691 DNA repair [AC自动机 DP]
- hdu 2457 DNA repair(ac自动机+dp)
- POJ 3691 DNA repair(AC自动机+DP)
- hdu 2457 DNA repair (ac自动机+dp)
- hdu_2457_DNA repair(AC自动机+DP)
- POJ 3691 DNA repair(AC自动机+DP)
- HDU 2457 DNA repair(AC自动机+DP)
- HDU 2457 DNA repair(AC自动机 + DP 入门)
- poj 3691 DNA repair(AC自动机+DP)
- HDU - 2457 DNA repair(AC自动机+DP)