您的位置:首页 > 其它

POJ1002-487-3279

2017-09-16 15:24 211 查看
#include<iostream>  

#include<map>  

#include<string>  

using namespace std;  

  

char convert(char character) {  

    switch (character) {  

        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';  

    }  

}  

  

int main()  

{  

    int N;  

    cin >> N;  

    map<string, int> count_telep;  

    for (int i = 0; i < N; i++) {  

        string input, tele;  

        cin >> input;  

        for (unsigned int j = 0; j < input.length(); j++) {  

            if (tele.length() == 3) tele += '-';  

            if (input[j] >= '0'&&input[j] <= '9')  

                tele += input[j];  

            if (input[j] >= 'A'&&input[j] <= 'Z')  

                tele += convert(input[j]);  

        }  

        if (count_telep.count(tele) == 0)  

            count_telep[tele] = 0;  

        count_telep[tele] += 1;  

    }  

    map<string, int>::iterator iter;  

    bool flag = false;  

    for (iter = count_telep.begin(); iter != count_telep.end(); iter++) {  

        if (iter->second > 1) {  

            cout << iter->first << ' ' << iter->second<< endl;  

            flag = true;  

        }  

    }  

    if (!flag)  

        cout << "No duplicates. ";  

    return 0;  

}

map为方便查找,按一定顺序存储,key为string类型时按照字典排序。

遍历map的两种方式:

定义为map<int,string>的元素遍历:

map<int,string> maps
for(int i=0;i<maps.size();i++){
string=maps[i];
}
定义为map<string,int>的元素遍历:

map<string,int> maps;
map<string,int>::iterator iter;
for(iter=maps.begin();iter!=maps.end();iter++){
string=iter->first;
int=iter->second;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: