本帖最後由 jimmyhealer 於 2022-5-16 14:03 編輯
嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 c/c++ 版,如果大家很熱絡的話再申請 演算法/競賽程式 版吧!
--------------------------------------------------- 正文開始 ---------------------------------------------------
題目名稱: String Problem
題目網站: Codefoces
題目網址: https://codeforces.com/problemset/problem/33/B
題目內容:
Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.
題目大意:
給你兩個字串,和一些轉換字元的規則及費用。
問你花最小的錢,讓兩個字串相等。
範例輸入 #1:
uayd
uxxd
3
a x 8
x y 13
d c 3
範例輸出 #1:
21
uxyd
範例輸入 #2:
a
b
3
a b 2
a b 3
b a 5
範例輸出 #2:
2b
--------------------------------------------------- 題解心得 ---------------------------------------------------
第一眼看到要最小花費,又有轉換以為是 DP,後來仔細想想字串前後沒有關係,所以直接單獨看一個字元的最小轉換花費。
接著可以思考如何找出最小花費,仔細研究轉換規則後可以轉成有向圖,有向圖就可以找最短路徑。
例如:
a → b → e (10)
c → e (20)
如上面這種情況,a 跟 c 要轉換成同一個字元 可以轉換成 e ,而花費會是 30。
AC code (30ms, 1.6 MB)- #include<bits/stdc++.h>
- #define ll long long
- #define INF 0x3f3f3f3f
- #define MOD 1000000007
- #define io iOS::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- using namespace std;
- const int MXN = 30;
- int path[MXN][MXN];
- int dis[MXN];
- void dijkstra(int s){
- memset(dis, INF, sizeof(dis));
- dis[s] = 0;
- priority_queue<pair<int, int>, vector<pair<int, int> > , greater<pair<int, int> > > pq;
- pq.push({dis[s], s});
- while(!pq.empty()){
- auto [w, u] = pq.top(); pq.pop();
- for(int v = 0; v < 26; ++v){
- if(dis[v] > dis[u] + path[u][v]){
- dis[v] = dis[u] + path[u][v];
- pq.push({dis[v], v});
- }
- }
- }
- }
- void sol(){
- ll ans = 0, n = 0, m = 0, k = 0;
- string s1, s2;
- cin >> s1 >> s2;
- cin >> n;
- memset(path, INF, sizeof(path));
- for(int i = 0; i < n; ++i){
- char u, v;
- int w;
- cin >> u >> v >> w;
- if(w < path[u - 'a'][v - 'a'])
- path[u - 'a'][v - 'a'] = w;
- }
- for(int i = 0; i < 26; ++i){
- dijkstra(i);
- for(int j = 0; j < 26; ++j){
- path[i][j] = dis[j];
- }
- }
- if(s1.size() != s2.size()){
- cout << "-1";
- return;
- }
- for(int i = 0; i < (int)s1.size(); ++i){
- int tmp = INF;
- char tmpc = s1[i];
- for(int j = 0; j < 26; ++j){
- if(tmp > path[s1[i] - 'a'][j] + path[s2[i] - 'a'][j]){
- tmp = path[s1[i] - 'a'][j] + path[s2[i] - 'a'][j];
- tmpc = j + 'a';
- }
- }
- s1[i] = tmpc;
- ans += tmp;
- }
- if(ans > INF){
- cout << "-1\n";
- return;
- }
- cout << ans << '\n';
- cout << s1 << '\n';
- }
- int main(){
- io
- int t = 1;
- while(t--){
- sol();
- }
- }
複製代碼
|