FZU-2172 DP

来源:互联网 发布:混凝土压力泌水率数据 编辑:程序博客网 时间:2024/05/21 17:27

大师兄在取经途中迷上了ACM-ICPC,稍不留神,师傅就被妖怪抓走了。

  大师兄并不着急去救师傅,在虐这道简单题:

  有两个字符串A和B,每一次可以选择以下操作中的一种,只对字符串A进行操作,用最少的操作使得字符串A与字符串B相等:

在字符串A中插入一个字符;

在字符串A中删除一个字符;

将字符串A复制,得到字符串A的一个拷贝C,将字符串C接在字符串A后面。

Input

  每组输入数据包含两行,第一个一个字符串A,第二行一个字符串B。输入字符串最大长度为10。

如果最少操作次数大于15,则输出”more than 15 operations.”。

Output

  对每组输入数据,输出最少的操作次数,使得字符串A与字符串B相等。

Sample Input
aaaaaacaaaaa
Sample Output
24



思路参考:http://www.cnblogs.com/13224ACMer/p/5276966.html


#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int maxn = 12;const int INF = 0x3f3f3f3f;char x[maxn], y[maxn];int dp[maxn][maxn];int main(){while (scanf("%s%s",x+1,y+1)!=EOF){memset(dp, INF, sizeof(dp));int n = strlen(x + 1);int m = strlen(y + 1);for (int i = 0; i <= n; i++)dp[0][i] = i;for (int i = 0; i <= m; i++)dp[i][0] = i;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (x[i] == y[j])dp[i][j] = dp[i - 1][j - 1];dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));if (j % 2 == 0){bool key = true;for (int k = 1; k <= j / 2; k++)if (y[k] != y[k + j / 2]){key = false;break;}if (key)dp[i][j] = min(dp[i][j / 2] + 1, dp[i][j]);}}}if (dp[n][m] > 15)puts("more than 15 operations.");elsecout << dp[n][m] << endl;}return 0;}




0 0
原创粉丝点击