POJ 1002

电话号码题,A了我一上午

本题关键是 map 数据类型的运用,mp[s]++

还有一点是 字符串的增加,只能写成 str += 的格式,我当初异想天开地以为 str = str + 也可以,后来发现太天真了。。

最后注意,输出,当没有重复的时候,记得输出 No duplicates.

#include<iostream>
#include<map>
#include<string>
#include<string.h>
using namespace std;

int fun(char x){
    switch(x){
        case 'A':
        case 'B':
        case 'C': return '2';
        case 'D':
        case 'E':
        case 'F': return '3';
        case 'G':
        case 'H':
        case 'I': return '4';
        case 'J':
        case 'K':
        case 'L': return '5';
        case 'M':
        case 'N':
        case 'O': return '6';
        case 'P':
        case 'R':
        case 'S': return '7';
        case 'T':
        case 'U':
        case 'V': return '8';
        case 'W':
        case 'X':
        case 'Y': return '9';
        default:
            return x;
    }
}

int main(){
    int n;
    int len;
    cin>>n;
    char str[20];
    map<string,int>mp;
    map<string,int>::iterator iter;
    for(int i=0;i<n;i++){
        string s = "";
        cin>>str;
        len = strlen(str);
        for(int j=0;j<len;j++)
            if((str[j]>='A'&&str[j]<='Z')||(str[j]>='0'&&str[j]<='9'))
                s += fun(str[j]);
        mp[s] += 1;
    }
    int counter = 1;
    for(iter = mp.begin();iter != mp.end();iter++){
        if(iter->second>1){
            counter++;
            for(int i=0;i<7;i++){
                cout<<iter->first[i];
                if(i==2)
                    cout<<'-';
            }
            cout<<" "<<iter->second<<endl;
        }
        
    }
    if(counter==1)
        cout<<"No duplicates."<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值