FZU-2172 DP

大师兄在取经途中迷上了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
a
aaaa
ac
aaaaa
Sample Output
2
4



思路参考: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.");
		else
			cout << dp[n][m] << endl;
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值