题解 | #找位置#

使用哈希表unordered_map
要点:
1.map.find(str[i])==map.end()判断是否存在
2.unordered_map<char,item> map复杂型hash表操作
#include<iostream>
#include<string>
#include<string>
#include<algorithm>
#include<cstdio>
#include<unordered_map>
#include<vector>
using namespace std;
typedef struct item{
    int count;
    vector<int> index;
};
int main(){
    string str;
    while(cin>>str){
        unordered_map<char,item> map;
        for(int i=0;i<str.length();i++){
            if(map.find(str[i])==map.end()){
                item t;
                t.count =1;
                t.index.push_back(i);
                map.insert({str[i],t});
            }else{
                map[str[i]].count+=1;
                map[str[i]].index.push_back(i);
                str[i]='#';
            }
        }
        for(int i=0;i<str.length();i++){
            if(map[str[i]].count>1){
                for(int k = 0;k<map[str[i]].index.size();k++){
                    if(k<map[str[i]].index.size()-1)
                    cout<<str[i]<<":"<<map[str[i]].index[k]<<",";
                    else{
                        cout<<str[i]<<":"<<map[str[i]].index[k]<<"\n";
                    }
                }
            }
        }
        
    }
    
}
全部评论

相关推荐

就在我现在公司的隔壁每天经过都唏嘘不已(就是羡慕)什么时候可以到这里上班啊
柯基在debug:从大学毕业投简历到现在了,应届的时候我都面到终面了,现在工作四年了连简历初筛都过不了了
投递莉莉丝游戏等公司8个岗位
点赞 评论 收藏
分享
Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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