AcWing 3610. 找位置
原题链接
简单
作者:
王杜杜
,
2025-01-14 21:34:22
,
所有人可见
,
阅读 1
C++ 代码
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::unordered_map;
using std::vector;
int main()
{
string str;
unordered_map<char, vector<int>> Map;
vector<char> vec;
cin >> str;
if (str.empty()) {
cout << "Empty input!" << endl;
return 0;
}
int len = str.length();
for (int i = 0; i < len; i++) {
if (Map[str[i]].size() == 0)
vec.push_back(str[i]);
Map[str[i]].push_back(i);
}
// 输出结果
for (auto elem : vec) {
string res;
for (size_t i = 0; i < Map[elem].size(); i++) {
res += elem;
res += ':';
res += std::to_string(Map[elem][i]);
if (i < Map[elem].size() - 1) { //省去最后一个逗号
res += ',';
}
}
res += '\n';
if(Map[elem].size() == 1)
continue;
cout << res;
}
return 0;
}