题解 | #找位置#
找位置
https://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; unordered_map<char, vector<int>> my_map; for (int i = 0; i < s.size(); ++i) my_map[s[i]].push_back(i); unordered_map<char, bool> isVisited; for (const char &c: s) { if (isVisited[c]) continue; isVisited[c] = true; const vector<int> &v = my_map[c]; if (v.size() > 1) { for (const int &j: v) { cout << c << ':' << j; if (j != v.back()) cout << ','; } cout << '\n'; } } return 0; }