题解 |用哈希表记录字符对应出现的位置(用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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:15
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务