動態規劃演算法,最小代價

2021-07-03 10:16:12 字數 1148 閱讀 9424

將乙個字串通過插入字串或者刪除字串的方式轉換為另乙個給定的字串。刪除連續n個字元的操作的代價為2,插入n個字元的代價為n+2。求轉換的最小代價。
package com.test;

import com.copy.test.demo1copytest;

public class demo

public static int del = -1;

public static int origal = 0;

public static int add = 1;

public static int getaddcount(int f, int type) else

return mincost;

} public static int getdelcount(int f, int type) else

return mincost;

} public static int getmin(int a, int b)

public static int getminexpenses(string astring, string bstring)

for (int i = 1; i < bstring.length() + 1; i++)

int type = origal;

for (int i = 1; i < astring.length() + 1; i++)

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)

else

if (mincost > f[i - 1][j - 1] + cost)

f[i][j] = mincost;

}// system.out.println("");

} return f[astring.length()][bstring.length()];

}}

動態規劃演算法

一 動態規劃演算法原理 將待求解的問題分解成若干個相互聯絡的子問題,先求解子問題,然後從這些子問題的解得到原問題的解 對於重複出現的子問題,只在第一次遇到的時候對它進行求解,並把答案儲存起來。了不去求解相同的子問題,引入乙個陣列,把所有子問題的解存於該陣列中,這就是動態規劃所採用的基本方法。動態規劃...

動態規劃演算法

動態規劃 通過把原問題分解為相對簡單的子問題來求解複雜問題。動態規劃常常適用於有重疊子問題和最優子結構性質的問題。演算法總體思想 演算法的基本步驟 演算法的基本要素 最優子結構 重疊子問題 備忘錄方法 問題描述 子串行 公共子串行 最長公共子串行 lcs 問題 問題分析 動態規劃求解lcs問題 最長...

動態規劃演算法

動態規劃演算法的思路 動態規劃法即 dynamic programming method dp 是系統分析中的種常用方法。動態規劃法是20世紀50年代由貝爾曼 r.bellman 等人提出的,用來解決多階段決策過程問題的一種最優化方法。多階段決策過程是指把研究問題分成若干個相互聯絡的階段,由每個階段...