HDU 3831 DICS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.