SRM 509 DIV1 500pt(DP)

題目簡述數組

給定一個字符串,能夠對其進行修改,刪除,增長操做,相應的操做有對應的花費,要求你用最小的花費把字符串變爲迴文串ide

題目作法spa

先搞一遍floyed把各類操做的最小花費求出來,而後就是相似編輯距離的DP了,這題坑了很久。。。中間結果會爆int,我設置的inf=0x3f3f3f3f,中間結果有inf+inf+inf..剛開始dp數組是int型的。。。這裏搞了好幾才發現這問題。。。西安現場賽也遇到這個問題了。。。而後也是浪費了將近100分鐘的時間。。。致使後面作題的時間不夠了。。。3d

代碼:code

 1 #define maxn 30
 2 int changeCost[maxn][maxn], addCost[maxn], eraseCost[maxn];
 3 LL dp[55][55];
 4 class PalindromizationDiv1
 5 {
 6 public:
 7     int getMinimumCost(string word, vector <string> operations)
 8     {
 9         memset(changeCost, 0x3f, sizeof(changeCost));
10         memset(addCost, 0x3f, sizeof(addCost));
11         memset(eraseCost, 0x3f, sizeof(eraseCost));
12         for (int i = 0; i < operations.size(); i++)
13         {
14             stringstream ss(operations[i]);
15             string s;
16             char a, b;
17             int num;
18             ss >> s;
19             if (s == "add")
20             {
21                 ss >> a >> num;
22                 addCost[a - 'a'] = num;
23             }
24             if (s == "erase")
25             {
26                 ss >> a >> num;
27                 eraseCost[a - 'a'] = num;
28             }
29             if (s == "change")
30             {
31                 ss >> a >> b >> num;
32                 changeCost[a - 'a'][b - 'a'] = num;
33             }
34         }
35         for (int i = 0; i < 26; i++) changeCost[i][i] = 0;
36         for (int k = 0; k < 26; k++)
37             for (int i = 0; i < 26; i++)
38                 for (int j = 0; j < 26; j++)
39                 {
40                     if (i == j || j == k || i == k) continue;
41                     changeCost[i][j] = min(changeCost[i][j], changeCost[i][k] + changeCost[k][j]);
42                 }
43 
44         for (int i = 0; i < 26; i++)
45             for (int j = 0; j < 26; j++)
46             {
47                 addCost[i] = min(addCost[i], addCost[j] + changeCost[j][i]);
48                 eraseCost[i] = min(eraseCost[i], changeCost[i][j] + eraseCost[j]);
49             }
50         int n = word.size();
51         memset(dp, 0x3f, sizeof(dp));
52         for (int i = n - 1; i >= 0; i--)
53         {
54             dp[i][i] = dp[i][i - 1] = 0;
55             for (int j = i + 1; j < n; j++)
56             {
57                 int a = word[i] - 'a', b = word[j] - 'a';
58                 dp[i][j] = min(dp[i][j], dp[i + 1][j] + eraseCost[a]);
59                 dp[i][j] = min(dp[i][j], dp[i][j - 1] + eraseCost[b]);
60                 for (int k = 0; k < 26; k++)
61                 {
62                     dp[i][j] = min(dp[i][j], dp[i + 1][j] + addCost[k] + changeCost[a][k]);
63                     dp[i][j] = min(dp[i][j], dp[i][j - 1] + addCost[k] + changeCost[b][k]);
64                     dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + changeCost[a][k] + changeCost[b][k]);
65                 }
66             }
67         }
68         LL ret = dp[0][n - 1];
69         return ret >= INF ? -1 : (int)ret;
70     }
71 };
View Code
相關文章
相關標籤/搜索