使用哈希表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";
}
}
}
}
}
}