在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP