题目地址
AC代码
MAP
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int n;
cin>>n;
map<string,int>list;
map<string,int,less<string> >ans;
while(n--){
string temp;
cin>>temp;
for(string::iterator it = temp.begin();it!=temp.end();it++){
if(*it=='-'){
it = temp.erase(it);
it--;
}
if(*it>=65&&*it<=67)*it='2';
if(*it>=68&&*it<=70)*it='3';
if(*it>=71&&*it<=73)*it='4';
if(*it>=74&&*it<=76)*it='5';
if(*it>=77&&*it<=79)*it='6';
if(*it>=80&&*it<=83)*it='7';
if(*it>=84&&*it<=86)*it='8';
if(*it>=87&&*it<=89)*it='9';
}
pair<map<string,int>::iterator,bool>judge;
judge = list.insert(make_pair(temp,0));
if(!judge.second){
temp.insert(3,"-");
pair<map<string,int>::iterator,bool>judge2;
judge2 = ans.insert(make_pair(temp,2));
if(!judge2.second)judge2.first->second++;
}
}
if(ans.empty()){
cout<<"No duplicates."<<endl;
}else{
for(map<string,int>::iterator itx=ans.begin();itx!=ans.end();itx++){
cout<<itx->first<<' '<<itx->second<<endl;
}
}
return 0;
}
桶+map
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
using namespace std;
int list[10000000];
int main() {
ios::sync_with_stdio(false);
long long int n;
cin>>n;
map<string,int,less<string> >ans;
while(n--){
string temp;
cin>>temp;
for(string::iterator it = temp.begin();it!=temp.end();it++){
if(*it=='-'){
it = temp.erase(it);
it--;
}
else if(*it>=65&&*it<=67)*it='2';
else if(*it>=68&&*it<=70)*it='3';
else if(*it>=71&&*it<=73)*it='4';
else if(*it>=74&&*it<=76)*it='5';
else if(*it>=77&&*it<=79)*it='6';
else if(*it>=80&&*it<=83)*it='7';
else if(*it>=84&&*it<=86)*it='8';
else if(*it>=87&&*it<=89)*it='9';
}
long long int num = 0;
for (int i = 0; i <7 ; ++i) {
num*=10;
num+=temp[i]-'0';
}
list[num]++;
if(list[num]!=1){
if(list[num]==2){
ans.insert(make_pair(temp,2));
}else{
ans[temp]++;
}
}
}
if(ans.empty()){
cout<<"No duplicates.";
return 0;
}
for(map<string,int>::iterator it = ans.begin();it!=ans.end();it++){
string x = it->first;
x.insert(3,"-");
cout<<x<<" "<<it->second<<endl;
}
return 0;
}
题解和题目思路
典型的查重题目,不清楚SET能不能做
用MAP的原因是map.insert()可以返回插入是否成功,如果插入失败就可以将这组string塞进另一个map中(map找元素很方便如果你想要用vector+pair我也不阻止你只要你跑的比XG记者快//滑稽)
等所有的元素都输入完了就可以用迭代器输出了