字符动态规划

顾小林 2015-06-20 10:39:12
将一个字符串通过插入字符串或者删除字符串的方式转换为另一个给定的字符串。删除连续n个字符的操作的代价为2,插入n个字符的代价为n+2。求转换的最小代价。
...全文
785 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
顾小林 2015-08-11
  • 打赏
  • 举报
回复
引用 9 楼 elfajj 的回复:
[quote=引用 7 楼 shen332401890 的回复:] [quote=引用 3 楼 FightForProgrammer 的回复:] 这个是dp问题把。找出地推关系
看上面的那个代码我觉得我写的没有问题 可是提交上去有错,但是又不告诉我怎么错。所以纠结在这里了[/quote] 第45-48行求解的ORIGAL操作(修改一个字符)的代价,是不是应该看成是删除+添加?这样代价cost的值有cost=+2+3=5和cost=+0+3=3两种情况,只是个想法,没验证过,不知道这个问题你解决没?[/quote] 好的我回头调试一下看看 谢谢
elfajj 2015-08-10
  • 打赏
  • 举报
回复
引用 7 楼 shen332401890 的回复:
[quote=引用 3 楼 FightForProgrammer 的回复:] 这个是dp问题把。找出地推关系
看上面的那个代码我觉得我写的没有问题 可是提交上去有错,但是又不告诉我怎么错。所以纠结在这里了[/quote] 第45-48行求解的ORIGAL操作(修改一个字符)的代价,是不是应该看成是删除+添加?这样代价cost的值有cost=+2+3=5和cost=+0+3=3两种情况,只是个想法,没验证过,不知道这个问题你解决没?
赵4老师 2015-06-29
  • 打赏
  • 举报
回复
搜“编辑距离 测试用例”
顾小林 2015-06-28
  • 打赏
  • 举报
回复
引用 3 楼 FightForProgrammer 的回复:
这个是dp问题把。找出地推关系
看上面的那个代码我觉得我写的没有问题 可是提交上去有错,但是又不告诉我怎么错。所以纠结在这里了
顾小林 2015-06-28
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
搜“编辑距离”

public static int getAddCount(int f, int type) {
		int minCost;
		if (type == ADD) {
			minCost = f + 1;
		} else {
			minCost = f + 3;
		}
		return minCost;
	}

	public static int getDelCount(int f, int type) {
		int minCost = 0;
		if (type == DEL) {
			minCost = f + 0;
		} else {
			minCost = f + 2;
		}
		return minCost;
	}

	public static int getMin(int a, int b) {
		return a < b ? a : b;
	}

	public static int GetMinExpenses(String aString, String bString) {
		int[][] f = new int[aString.length() + 1][bString.length() + 1];//f[i][j] 从a[i] -> b[j]的最小代价
		f[0][0] = 0;
		int operator[][] = new int[aString.length() + 1][bString.length() + 1];//用于记录操作

		for (int i = 1; i < aString.length() + 1; i++) {
			f[i][0] = 2;
			operator[i][0] = DEL;
		}
		for (int i = 1; i < bString.length() + 1; i++) {
			f[0][i] = 2 + i;
			operator[0][i] = ADD;
		}
		int type = ORIGAL;
		for (int i = 1; i < aString.length() + 1; i++) {
			// System.out.println();
			for (int j = 1; j < bString.length() + 1; j++) {
				// System.out.print(minCost + "_" + operator[i][j] + " ");
				int tempType;
                int cost = 0;
                if (aString.charAt(i - 1) != bString.charAt(j - 1))
                {
                    cost = 5;
                }
                int minCost;
                int delCount = getDelCount(f[i - 1][j], operator[i - 1][j]);
                int addCount = getAddCount(f[i][j - 1], operator[i][j - 1]);
                if (delCount >= addCount)
                {
                    operator[i][j] = ADD;
                    minCost = addCount;
                }
                else
                {
                    operator[i][j] = DEL;
                    minCost = delCount;
                }
                if (minCost > f[i - 1][j - 1] + cost)
                {
                    operator[i][j] = ORIGAL;
                    minCost = f[i - 1][j - 1] + cost;
                }
                f[i][j] = minCost;
			}
			// System.out.println("");
		}
		return f[aString.length()][bString.length()];
	}
顾小林 2015-06-27
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
搜“编辑距离”
编辑距离那个我看明白了,我也写了一段代码本地 本地测试OK,提交到(一个考试平台)就报错,也不给错的用例 所以我纠结了
赵4老师 2015-06-25
  • 打赏
  • 举报
回复
搜“编辑距离”
FightForProgrammer 2015-06-25
  • 打赏
  • 举报
回复
这个是dp问题把。找出地推关系
fly_dragon_fly 2015-06-24
  • 打赏
  • 举报
回复
cost(i,j),i为源长度,j为目标长度
顾小林 2015-06-20
  • 打赏
  • 举报
回复
没有想明白 这个递归关系 求指点一下

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧