简答的哈希
#include <iostream> #include <map> #include <vector> #include <set> using namespace std; int main() { string s; while (cin >> s) { map<char, vector<int>> m; set<char> st; for (int i = 0; i < s.length(); i++) { m[s[i]].push_back(i); } for (auto c: s) { if (st.find(c) == st.end()) { vector<int> t = m[c]; if (t.size() <= 1) continue; for (int i = 0; i < t.size(); i++) { if (i == 0) { cout << c << ":" << t[i]; } else { cout << "," << c << ":" << t[i]; } } cout << endl; st.insert(c); } } } }