AcWing 3610. 找位置
原题链接
简单
作者:
故事里的大魔王
,
2025-01-11 11:10:09
,
所有人可见
,
阅读 1
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string word;
cin >> word;
vector<char> firstAppear; // 记录元素出现的先后位置
set<char> appear;
multimap<char, int> dict; // 记录出现下标
unordered_map<char, int> count; // 记录出现次数
unordered_map<char, int>::iterator it; // 迭代器
multimap<char, int>::iterator mit; // 迭代器
// 记录dict, count
for (int i = 0; i < word.size(); ++i) {
dict.insert({ word[i], i });
it = count.find(word[i]);
if (it == count.end()) count.insert({ word[i], 1});
else it->second++;
if (appear.find(word[i]) == appear.end()) {
firstAppear.push_back(word[i]);
appear.insert(word[i]);
}
}
// 删除重复元素
for (it = count.begin(); it != count.end(); ++it)
if (it->second == 1)
dict.erase(it->first);
// 输出打印
for (int i = 0; i < firstAppear.size(); ++i) {
mit = dict.lower_bound(firstAppear[i]);
while (mit != dict.upper_bound(firstAppear[i])) {
cout << mit->first << ":" << mit->second;
++mit;
if (mit != dict.upper_bound(firstAppear[i]))
cout << ",";
}
if(dict.find(firstAppear[i]) != dict.end()) cout << endl;
}
return 0;
}