题解 |用哈希表记录字符对应出现的位置(用vector保存)
找位置
https://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <map> #include <vector> using namespace std; map<char, vector<int>>hashmap; int main() { char a[100]; while (scanf("%s", a) != EOF) { for (int i = 0; a[i] != '\0'; i++) { hashmap[a[i]].push_back(i); } for (int i = 0; a[i] != '\0'; i++) { if (hashmap[a[i]].size() > 1 && hashmap[a[i]][0] != 9999) { printf("%c:%d", a[i], hashmap[a[i]][0]); for (int j = 1; j < hashmap[a[i]].size(); j++) { printf(",%c:%d", a[i], hashmap[a[i]][j]); } hashmap[a[i]][0] = 9999; printf("\n"); } } } return 0; }