hdu 2457 DNA repair ac自动机+dp
来源:互联网 发布:商机助理怎么传淘宝 编辑:程序博客网 时间:2024/06/02 13:29
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2457
109ms
#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include<queue>using namespace std;typedef unsigned long long LL;const int INF = 0x3f3f3f3f;const int SN = 4;const int MN = 1005;int id['Z'];class AAM{public: int ch[MN][SN],tag[MN],fail[MN]; int root,cnt; void init(){ cnt = 0,root = cnt++; memset(ch[0],0,sizeof(ch[0])); memset(tag,0,sizeof(tag)); memset(fail,0,sizeof(fail)); } void insert(char *s){ int p = root; while(*s){ int index = id[*s]; if(tag[p]) return;//小小的优化 if(!ch[p][index]){ ch[p][index] = cnt; memset(ch[cnt],0,sizeof(ch[cnt])); ++cnt; } p = ch[p][index]; s++; } tag[p] = 1; } void getfail(){ queue<int> Q; for(int i = 0; i < SN; ++i){//两个数组都有清零不用再赋0给他了 if(ch[0][i]){ Q.push(ch[0][i]); } } while(!Q.empty()){ int u = Q.front();Q.pop(); if(tag[fail[u]]) tag[u] = 1; for(int i = 0;i < SN; ++i){ int &v = ch[u][i]; if(v){ fail[v] = ch[fail[u]][i]; Q.push(v); }else v = ch[fail[u]][i]; } } }};char s[1005];int dp[MN][MN];int n;AAM T;int main(){ int cas = 1; id['A'] = 0;id['T'] = 1;id['G'] = 2;id['C'] = 3; while(scanf("%d",&n),n){ T.init(); memset(dp,0x3f,sizeof(dp)); for(int i = 0; i < n; ++i){ scanf("%s",s); T.insert(s); } T.getfail(); scanf("%s",s); dp[0][0] = 0; int len = strlen(s); for(int i = 1; i <= len; ++i) for(int j = 0; j < T.cnt; ++j) for(int k = 0; k < 4; ++k){ int idx = T.ch[j][k]; if(T.tag[idx]) continue; dp[i][idx] = min(dp[i][idx],dp[i - 1][j] + (k != id[s[i - 1]]) ); } int ans = INF; for(int i = 0; i < T.cnt; ++i) ans = min(ans,dp[len][i]); printf("Case %d: %d\n",cas++,ans == INF? -1:ans); } return 0;}
0 0
- hdu 2457 DNA repair(AC自动机+DP)
- HDU 2457 DNA repair(AC自动机+DP)
- 【HDU】2457 DNA repair AC自动机+DP
- hdu 2457 DNA repair ac自动机+dp
- Hdu 2457 DNA repair (ac自动机+dp)
- [AC自动机+dp] hdu 2457 DNA repair
- hdu 2457 DNA repair(AC自动机+DP)
- hdu 2457 DNA repair (ac自动机+dp)
- hdu 2457 DNA repair(ac自动机+dp)
- HDU 2457 DNA repair AC自动机+DP
- HDU - 2457 DNA repair AC自动机+dp
- Hdu 2457 DNA repair AC自动机+DP
- HDU 2457 DNA repair(AC自动机+DP)
- hdu DNA repair(AC自动机+DP)
- HDU 2457 DNA repair(AC自动机+DP)
- Poj 3691 & Hdu 2457 DNA repair (AC自动机+DP)
- HDU 2457 DNA repair(AC自动机 + DP 入门)
- POJ 3691 & HDU 2457 DNA repair (AC自动机,DP)
- java面试宝典-传值和传引用
- Python核心编程 练习 第四章
- iPhone开发之网络编程 AsyncSocket
- Android 应用程序之道
- storyboard取得视图控制器
- hdu 2457 DNA repair ac自动机+dp
- android最新环境搭建的问题!
- 无法访问google的简单解决办法
- 超级详细01背包
- Raft一致性算法
- MFC对键盘消息的截取与响应
- hdu 1028 Ignatius and the Princess III
- JAVA对Excel的操作小例子一则
- Java面试题