在 C++ 中製作一個泛字母字串的成本


在本教程中,我們將討論一個程式,以查詢製作泛字母字串的成本。

為此,我們將獲得一個整數陣列。我們的任務是將給定的字串轉換為泛字母,並藉助於提供新增字元成本的陣列來計算執行此操作的成本。

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
//calculating the total cost of
//making panagram
int calc_cost(int arr[], string str) {
   int cost = 0;
   bool occurred[26] = { false };
   for (int i = 0; i < str.size(); i++)
      occurred[str[i] - 'a'] = true;
   for (int i = 0; i < 26; i++) {
      if (!occurred[i])
         cost += arr[i];
   }
   return cost;
}
int main(){
   int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
   string str = "abcdefghijklmopqrstuvwz";
   cout << calc_cost(arr, str);
   return 0;
}

輸出

63

更新時間: 2020 年 2 月 5 日

129 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始操作
廣告
© . All rights reserved.