DNA repair HDU - 2457 AC鑷姩鏈?DP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DNA repair HDU - 2457 AC鑷姩鏈?DP相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/date' title='date'>date   bit   sig   src   pre   琛ㄧず   ext   cti   

棰樻剰锛?/p>

缁欎綘N涓ā鏉夸覆,骞朵笖缁欎綘涓€涓枃鏈覆,

鐜板湪闂綘杩欎釜鏂囨湰涓叉渶灏戦渶瑕佹敼鍙樺嚑涓瓧绗︽墠鑳戒娇寰楀畠涓嶅寘鍚换浣曟ā鏉夸覆.

(浠ヤ笂瀛楃鍙敱A,T,G,C鏋勬垚)

 

棰樿В锛?/p>

鍒氬紑濮嬪仛杩欎竴棰樼殑鏃跺€欒〃绀哄緢鎳甸€硷紝濂藉儚娌℃湁瀛﹁繃杩欑绫诲瀷鐨勯棶棰樸€?/p>

鍚庨潰浠旂粏鎯虫兂锛屽湪涔嬪墠鐨勯鐩腑锛屽浼氫簡姹傚嚭涓嶅寘鍚换浣曟ā鏉夸覆鐨勬柟妗堟暟銆?/p>

杩欓鍙互杞寲涓嬶紝姹傚嚭鎵€鏈変笉鍖呭惈浠讳綍妯℃澘涓茬殑鏂规涓笌鍘熶覆鏈€灏戠殑涓嶅悓鏁扮洰銆?/p>

鏍规嵁杩欎釜DP

dp銆恑銆戙€恓銆?琛ㄧず璧板埌闀垮害涓?i 鐨勬椂鍊欙紝鍦ˋC鑷姩鏈?j 杩欎釜鑺傜偣涓婃渶澶氫笌鍘熶覆涓嶅悓鐨勪釜鏁般€?/p>

鐒跺悗杩欓灏卞彉鎴怱B棰樹簡銆?/p>

 

鎶€鏈浘鐗? id=
  1 #include <set>
  2 #include <map>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstring>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <unordered_map>
 14 
 15 #define  pi    acos(-1.0)
 16 #define  eps   1e-9
 17 #define  fi    first
 18 #define  se    second
 19 #define  rtl   rt<<1
 20 #define  rtr   rt<<1|1
 21 #define  bug                printf("******\n")
 22 #define  mem(a, b)          memset(a,b,sizeof(a))
 23 #define  name2str(x)        #x
 24 #define  fuck(x)            cout<<#x" = "<<x<<endl
 25 #define  sfi(a)             scanf("%d", &a)
 26 #define  sffi(a, b)         scanf("%d %d", &a, &b)
 27 #define  sfffi(a, b, c)     scanf("%d %d %d", &a, &b, &c)
 28 #define  sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
 29 #define  sfL(a)             scanf("%lld", &a)
 30 #define  sffL(a, b)         scanf("%lld %lld", &a, &b)
 31 #define  sfffL(a, b, c)     scanf("%lld %lld %lld", &a, &b, &c)
 32 #define  sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
 33 #define  sfs(a)             scanf("%s", a)
 34 #define  sffs(a, b)         scanf("%s %s", a, b)
 35 #define  sfffs(a, b, c)     scanf("%s %s %s", a, b, c)
 36 #define  sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
 37 #define  FIN                freopen("../date.txt","r",stdin)
 38 #define  gcd(a, b)          __gcd(a,b)
 39 #define  lowbit(x)          x&-x
 40 #define  IO                 iOS::sync_with_stdio(false)
 41 
 42 
 43 using namespace std;
 44 typedef long long LL;
 45 typedef unsigned long long ULL;
 46 const ULL seed = 13331;
 47 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
 48 const int maxn = 1e6 + 7;
 49 const int maxm = 8e6 + 10;
 50 const int INF = 0x3f3f3f3f;
 51 const int mod = 1e9 + 7;
 52 
 53 
 54 char str[55][22], buf[1010];
 55 int n, dp[1010][1010];
 56 
 57 int get_num(char ch) 
 58     if (ch == 鈥?/span>A鈥?/span>) return 0;
 59     if (ch == 鈥?/span>T鈥?/span>) return 1;
 60     if (ch == 鈥?/span>C鈥?/span>) return 2;
 61     if (ch == 鈥?/span>G鈥?/span>) return 3;
 62 
 63 
 64 struct Aho_Corasick 
 65     int next[1010][4], fail[1010], End[1010];
 66     int root, cnt;
 67 
 68     int newnode() 
 69         for (int i = 0; i < 4; i++) next[cnt][i] = -1;
 70         End[cnt++] = 0;
 71         return cnt - 1;
 72     
 73 
 74     void init() 
 75         cnt = 0;
 76         root = newnode();
 77     
 78 
 79     void insert(char buf[]) 
 80         int len = strlen(buf);
 81         int now = root;
 82         for (int i = 0; i < len; i++) 
 83             if (next[now][get_num(buf[i])] == -1) next[now][get_num(buf[i])] = newnode();
 84             now = next[now][get_num(buf[i])];
 85         
 86         End[now]++;
 87     
 88 
 89     void build() 
 90         queue<int> Q;
 91         fail[root] = root;
 92         for (int i = 0; i < 4; i++)
 93             if (next[root][i] == -1) next[root][i] = root;
 94             else 
 95                 fail[next[root][i]] = root;
 96                 Q.push(next[root][i]);
 97             
 98         while (!Q.empty()) 
 99             int now = Q.front();
100             Q.pop();
101              if (End[fail[now]]) End[now] = 1;
102             for (int i = 0; i < 4; i++)
103                 if (next[now][i] == -1) next[now][i] = next[fail[now]][i];
104                 else 
105                     fail[next[now][i]] = next[fail[now]][i];
106                     Q.push(next[now][i]);
107                 
108         
109     
110 
111     int solve() 
112         int len = strlen(buf);
113         for (int i = 0; i <= len; ++i)
114             for (int j = 0; j < cnt; ++j)
115                 dp[i][j] = INF;
116         dp[0][0] = 0;
117         for (int i = 0; i < len; ++i) 
118             for (int j = 0; j < cnt; ++j) 
119                 if (dp[i][j] == INF || End[j]) continue;
120                 for (int k = 0; k < 4; ++k) 
121                     int idx = next[j][k];
122                     if (End[idx]) continue;
123                     dp[i + 1][idx] = min(dp[i + 1][idx], dp[i][j] + (get_num(buf[i]) != k));
124                 
125             
126         
127         int ans = INF;
128         for (int i = 0; i < cnt; ++i) ans = min(ans, dp[len][i]);
129         if (ans == INF) return -1;
130         return ans;
131     
132 
133     void debug() 
134         for (int i = 0; i < cnt; i++) 
135             printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
136             for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
137             printf("]\n");
138         
139     
140  ac;
141 
142 
143 int main() 
144     //FIN;
145     int cas = 1;
146     while (sfi(n) && n) 
147         ac.init();
148         for (int i = 1; i <= n; ++i) 
149             sfs(str[i]);
150             ac.insert(str[i]);
151         
152         ac.build();
153         sfs(buf);
154         printf("Case %d: %d\n", cas++, ac.solve());
155     
156     return 0;
157 
View Code

 

以上是关于DNA repair HDU - 2457 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

POJ3691DNA repair(AC自动机,DP)