'commit'
This commit is contained in:
75
TangDou/XiTi/1398.cpp
Normal file
75
TangDou/XiTi/1398.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//替换字符串
|
||||
string &replace_all_distinct(string &str, const string &old_value, const string &new_value) {
|
||||
for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
|
||||
if ((pos = str.find(old_value, pos)) != string::npos)
|
||||
str.replace(pos, old_value.length(), new_value);
|
||||
else break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
//分割字符串
|
||||
void split(const string &s, vector<string> &sv, const char flag = ' ') {
|
||||
sv.clear();
|
||||
istringstream iss(s);
|
||||
string temp;
|
||||
|
||||
while (getline(iss, temp, flag)) {
|
||||
//sv.push_back(stoi(temp)); //如果需要返回整数
|
||||
sv.push_back(temp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//unordered_map 按value进行排序
|
||||
bool comp(const pair<string, int> &a, const pair<string, int> &b) {
|
||||
return a.second > b.second;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//输入+输出重定向
|
||||
//freopen("../x.in", "r", stdin);
|
||||
//freopen("../x.out", "w", stdout);
|
||||
|
||||
string s;
|
||||
getline(cin, s);
|
||||
|
||||
string needReplaceArr[4] = {"?", ".", ",", "!"};
|
||||
//去掉无用的标点符号
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
replace_all_distinct(s, needReplaceArr[i], "");
|
||||
}
|
||||
//全部转小写!
|
||||
transform(s.begin(), s.end(), s.begin(), ::tolower);
|
||||
//按空格进行分割
|
||||
vector<string> v1;
|
||||
split(s, v1, ' ');
|
||||
//声明uno
|
||||
unordered_map<string, int> _map;
|
||||
for (int i = 0; i < v1.size(); ++i) {
|
||||
if (v1[i] != "") _map[v1[i]]++;
|
||||
}
|
||||
//遍历_map,找到值最大的
|
||||
vector<pair<string, int> > b;
|
||||
for (auto x:_map) {
|
||||
b.push_back(x);
|
||||
}
|
||||
//通过自定义排序方式进行排序
|
||||
sort(b.begin(), b.end(), comp);
|
||||
|
||||
//遍历的方法
|
||||
//for(auto x:b)
|
||||
// cout<< x.first<<" " <<x.second <<" "<<endl;
|
||||
|
||||
//输出结果
|
||||
cout << b[0].first << " " << b[0].second << endl;
|
||||
|
||||
//关闭文件
|
||||
//fclose(stdin);
|
||||
//fclose(stdout);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user