Question:
The four operations given in the question are transformed from string a to string B with the minimum number of times.
Ideas:
Since there are only four types of operations, we can determine that it is correct to match a and B from the ground up.
How many states are there? Total length [a] * length [B] * (1 + num (~ Z) + num (~ Z) The status is not much and can be solved using DP.
The above calculation status can be expressed as DP [I] [J] [K], that is, string a matches string I, and string B matches string J K to indicate the characters modified by the suffix modification operation.
Then, you only need to name all the DP tables and use DP + (Lena-I) + (lenb-j) to update ans.
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef unsigned long long LL;#define N 505#define M 53#define inf 100000000int ans;int dp[N][N][M];char f[N], g[N];int change(char u) {if (u >= 'A' && u <= 'Z')return u - 'A' + 1;if (u >= 'a' && u <= 'z')return u - 'a' + 27;return 0;}int main() {int i, j, k, ans, lf, lg;while (~scanf("%s", f)) {if (!strcmp(f, "#"))break;scanf("%s", g);lf = strlen(f);lg = strlen(g);for (i = 0; i <= lf; i++) {for (j = 0; j <= lg; j++) {for (k = 0; k < M; k++)dp[i][j][k] = inf;}}ans = inf;dp[0][0][0] = 0;for (i = 0; i <= lf; i++) {for (j = 0; j <= lg; j++) {for (k = 0; k < M; k++) {if (dp[i][j][k] == inf)continue;ans = min(ans, dp[i][j][k] + lf - i + lg - j);if (i == lf || j == lg)continue;if ((!k && f[i] == g[j]) || (k && k == change(g[j]))) {//samedp[i + 1][j + 1][k] = min(dp[i + 1][j + 1][k],dp[i][j][k]);} else {//deletedp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + 1);//insertdp[i][j + 1][k] = min(dp[i][j + 1][k], dp[i][j][k] + 1);//changedp[i + 1][j + 1][k] = min(dp[i + 1][j + 1][k],dp[i][j][k] + 1);//Suffix changedp[i + 1][j + 1][change(g[j])] = min(dp[i + 1][j + 1][change(g[j])],dp[i][j][k] + 1);}}}}printf("%d\n", ans);}return 0;}
HDU 3831 DICS