#include <iostream> #include <map> #include <algorithm> using namespace std; const int NR = 110; int a[NR], b[NR], tot; string s[NR]; map<string, int> cnt; map<string, bool> vis; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); for (int i = 1; i <= m; i++) cin >> s[i], cnt[s[i]]++; for (int i = 1; i <= m; i++) if (!vis[s[i]]) b[++tot] = cnt[s[i]], vis[s[i]] = true; sort(b + 1, b + tot + 1, greater<int>()); int ans1 = 0, ans2 = 0; for (int i = 1; i <= tot; i++) ans1 += b[i] * a[i]; sort(a + 1, a + n + 1, greater<int>()); // sort(b + 1, b + tot + 1); for (int i = 1; i <= tot; i++) ans2 += a[i] * b[i]; cout << ans1 << " " << ans2 << endl; return 0; }
时间: 2024-04-27 10:23:49 浏览: 7
这段代码是一个统计字符串中字母出现次数并计算两种不同排序方式下的结果的程序,具体实现过程如下:
1. 读入两个整数n和m,分别表示正整数序列a的长度和字符串序列s的长度。
2. 读入正整数序列a,然后将其从小到大排序。
3. 读入字符串序列s,然后使用map<string, int>来记录每个字符串出现的次数。
4. 对于每个出现过的字符串,如果它还没有被处理过,就将其出现次数添加到一个数组b中,并标记为已处理。
5. 对数组b从大到小排序。
6. 使用两种不同的排序方式计算结果,并输出它们。
具体的实现细节可以参考代码中的注释。
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include <iostream> #include<algorithm> #include<cmath> #include <queue> using namespace std;
#include <iostream>:这是C++标准库中的头文件,用于输入输出流操作,包括cin、cout等。
#include<algorithm>:这是C++标准库中的头文件,用于提供各种算法操作,如排序、查找等。
#include<cmath>:这是C++标准库中的头文件,用于提供数学函数操作,如绝对值、平方根等。
#include <queue>:这是C++标准库中的头文件,用于提供队列操作,包括入队、出队等。
using namespace std;:这是C++的命名空间声明,表示使用std命名空间,可以直接使用std中的函数和对象。