pku 3356 AGTC

#include <iostream> #include <string> using namespace std; int v[1001][1001]; int main() { int m, n; string s1, s2; while(scanf("%d", &m)!=EOF) { cin >> s1; scanf("%d", &n); cin >> s2; for(int i = 1; i <= n; ++i) v[0][i] = i; for(int i = 1; i <= m; ++i) v[i][0] = i; for(int i = 1; i <= m; ++i) for(int j = 1; j <= n; ++j) if(s1[i-1] == s2[j-1]) v[i][j] = min(min(v[i-1][j-1], v[i-1][j]+1), v[i][j-1]+1); else v[i][j] = min(min(v[i-1][j-1]+1, v[i-1][j]+1), v[i][j-1]+1); printf("%d/n", v[m][n]); } return 0; } /* min{v[i-1][j-1], v[i-1][j]+1, v[i][j-1]+1} (s[i] = s[j]) v[i][j] = min{v[i-1][j-1]+1, v[i-1][j]+1, v[i][j-1]+1} (s[i] != s[j]) //1.s[i] = s[j],其中v[i-1][j-1]+1表示不作处理,v[i-1][j]+1表示删除字符s[i], v[i][j-1]+1表示在s[i]后添加s[j] //s[i] != s[j],其中v[i-1][j-1]+1表示把s[i]变成s[j],v[i-1][j]+1表示删除字符s[i], v[i][j-1]+1表示在s[i]后添加s[j] //2.注意初始化的处理 result: Problem: 3356 User: xiaofengsheng Memory: 4288K Time: 32MS Language: G++ Result: Accepted */

你可能感兴趣的:(pku 3356 AGTC)