栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3691 DNA repair

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3691 DNA repair

#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAX_N = 50, MAX_P = 20, MAX_W = 1000, SON = 4, INF = 0x3f3f3f3f;char t[MAX_P + 1], s[MAX_W + 1];int n, idx[256];int f[2][MAX_N * MAX_P + 1], now, pre;struct node_t {    node_t *son[SON], *fail;    bool v;} node_pool[MAX_N * MAX_P + 1], *node_idx, *root;node_t *node_alloc() {    node_t *ret = node_idx ++;    memset(ret -> son, 0, sizeof(ret -> son));    ret -> fail = NULL;    ret -> v = 0;    return ret;}void init() {    node_idx = node_pool;    root = node_alloc();}void ins(char *str) {    node_t *pos = root;    while (*str) {        int p = idx[*(str ++)];        if (!pos -> son[p]) pos -> son[p] = node_alloc();        pos = pos -> son[p];    }    pos -> v = 1;}void build() {    static queue <node_t *> q;    for (int i = 0; i < SON; i ++)        if (root -> son[i]) { root -> son[i] -> fail = root; q.push(root -> son[i]);        }        else root -> son[i] = root;    while (q.size()) {        node_t *u = q.front();        q.pop();        for (int i = 0; i < SON; i ++) if (u -> son[i]) {     u -> son[i] -> fail = u -> fail -> son[i];     u -> son[i] -> v |= u -> fail -> son[i] -> v;     q.push(u -> son[i]); } else u -> son[i] = u -> fail -> son[i];    }}int main() {    idx['A'] = 0, idx['G'] = 1, idx['C'] = 2, idx['T'] = 3;    int cas = 0;    while (scanf("%d", &n) != EOF && n) {        init();        for (int i = 0; i < n; i ++) { scanf("%s", t); ins(t);        }        build();        scanf("%s", s);        int sz = strlen(s);        int cnt = node_idx - node_pool;        now = 0, pre = 1;        memset(f[now], 0x3f, sizeof(f[now]));        f[now][0] = 0;        for (int i = 0; i < sz; i ++) { now ^= 1, pre ^= 1; memset(f[now], 0x3f, sizeof(f[now])); for (int j = 0; j < cnt; j ++)     if (f[pre][j] < INF) {         node_t *pos = node_pool + j;         for (int k = 0; k < 4; k ++)  if (!pos -> son[k] -> v) {      int t = pos -> son[k] - node_pool;      f[now][t] = min(f[now][t], f[pre][j] + (k != idx[s[i]]));  }     }        }        printf("Case %d: ", ++ cas);        int ans = INF;        for (int i = 0; i < cnt; i ++) if (!(node_pool + i) -> v) ans = min(ans, f[now][i]);        if (ans == INF) printf("-1n");        else printf("%dn", ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号