004:DNA repair

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44489823/article/details/102767734

总时间限制: 2000ms 内存限制: 65536kB
描述
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.

输入
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.

输出
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.
样例输入
2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0
样例输出
Case 1: 1
Case 2: 4
Case 3: -1

#include <string.h>
#include <algorithm>
#include <stack>
#include <string>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <fstream>
#include <set>
#define inf 0x3f3f3f3f

using namespace std;
const int maxn = 1005;
int n,nodecou,l;
char str[maxn];
int gen[85],f[maxn][maxn];

struct node {
    int next[4];
    int prev;
    bool isdanger;
    char val;
    node() {
        memset(next, 0, sizeof(next));
        prev = -1;
        isdanger = false;
        val = 0;
    }
}tree[maxn];

void insert() {
    int now = 1,l=strlen(str);
    for (int i = 0; i < l; i++) {
        int idx = gen[str[i]];
        if (tree[now].next[idx] == 0) {
            nodecou++;
            tree[now].next[idx] = nodecou;
            tree[nodecou].val = str[i];
        }
        now = tree[now].next[idx];
        if (i == l - 1)
            tree[now].isdanger = true;
    }
}

void build() {
    for(int i=0;i<4;i++)
        tree[0].next[i] = 1;
    tree[1].prev = 0;
    queue<int>q;
    q.push(1);
    while (!q.empty()) {
        int now = q.front(); q.pop();
        for (int i = 0; i < 4; i++) {
            int child = tree[now].next[i];
            int prev = tree[now].prev;
            if (child) {
                while (tree[prev].next[i] == NULL)
                    prev = tree[prev].prev;
                tree[child].prev = tree[prev].next[i];
                if (tree[tree[child].prev].isdanger)
                    tree[child].isdanger = true;
                q.push(child);
            }
            else {
                while (!tree[prev].next[i])
                    prev = tree[prev].prev;
                tree[now].next[i] = tree[prev].next[i];
                if (tree[child].isdanger)
                    tree[now].next[i] = 0;
            }
        }
    }
}

void dp(int kase) {
    for (int i = 1; i <= l; i++) {
        for (int j = 1; j <= nodecou; j++)
        {
            for (int k = 0; k < 4; k++)
                if (tree[j].next[k] && !tree[tree[j].next[k]].isdanger)
                    f[i][tree[j].next[k]] = min(f[i - 1][j] + (gen[str[i-1]]!=k), f[i][tree[j].next[k]]);
        }
    }
    int ans = inf;
    for (int i = 1; i <= nodecou; i++)
        ans = min(ans, f[l][i]);
    if (ans < inf)
        printf("Case %d: %d\n", kase, ans);
    else
        printf("Case %d: -1\n", kase);
}

void init() {
    int kase = 0;
    while (scanf("%d",&n)) {
        nodecou = 1;
        if (n == 0)return;
        kase++;
        memset(tree, 0, sizeof(tree));
        memset(f, 0, sizeof(f));
        while (n--)
        {
            scanf("%s", str);
            insert();
        }
        build();
        scanf("%s", str);
        l = strlen(str);
        for (int i = 0; i <= l; i++)
            for (int j = 1; j <= nodecou; j++)
                f[i][j] = inf;
        f[0][1] = 0;
        dp(kase);
    }
}

int main()
{
   // freopen("myout.txt", "w", stdout);
    gen['A'] = 0, gen['G'] = 1, gen['C'] = 2, gen['T'] = 3;
    init();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44489823/article/details/102767734