关于C++的词汇统计
输入若干行,每行都有一条密码,最多100000行。输出:出现最多的3条密码,按照出现次数从大到小排序,若次数相同按照ASCII编码顺序。保证至少有3条不同密码。
输入样例:
123456
qwerty
12345678
123456
111111
1234567890
qwerty
123456
password
123123
987654321
输出样例:
123456
qwerty
111111
```c++
#include
using namespace std;
const int N=100009;
struct pwd{
string str;
int c;
};
pwd f[N];
bool cmp(const pwd&a,const pwd&b){
return a.c>b.c||a.c==b.c&&a.strint main()
{
int n=0;
mapint>::iterator mit;
for(mit=d.begin();mit!=d.end();mit++)
{
f[n].str=mit->first;
f[n].c=mit->second;
n++;
}
sort(f,f+n,cmp);
for(int i=0;i<3;i++)
{
cout<return 0;
}
```
“该回答引用ChatGPT”
代码如下
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct Node {
string password;
int cnt;
};
bool cmp(Node a, Node b) {
if (a.cnt != b.cnt) return a.cnt > b.cnt;
return a.password < b.password;
}
int main() {
map<string, int> m;
string password;
while (cin >> password) {
m[password]++;
}
vector<Node> res;
for (auto item : m) {
res.push_back({item.first, item.second});
}
sort(res.begin(), res.end(), cmp);
for (int i = 0; i < 3 && i < res.size(); i++) {
cout << res[i].password << endl;
}
return 0;
}