POJ1002-487-3279

来源:互联网 发布:湖南辉达 知乎 编辑:程序博客网 时间:2024/06/11 18:30
  1. #include<iostream>  
  2. #include<map>  
  3. #include<string>  
  4. using namespace std;  
  5.   
  6. char convert(char character) {  
  7.     switch (character) {  
  8.         case 'A':case 'B':case 'C': return '2';  
  9.         case 'D':case 'E':case 'F': return '3';  
  10.         case 'G':case 'H':case 'I': return '4';  
  11.         case 'J':case 'K':case 'L': return '5';  
  12.         case 'M':case 'N':case 'O': return '6';  
  13.         case 'P':case 'R':case 'S': return '7';  
  14.         case 'T':case 'U':case 'V': return '8';  
  15.         case 'W':case 'X':case 'Y': return '9';  
  16.     }  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     int N;  
  22.     cin >> N;  
  23.     map<string, int> count_telep;  
  24.     for (int i = 0; i < N; i++) {  
  25.         string input, tele;  
  26.         cin >> input;  
  27.         for (unsigned int j = 0; j < input.length(); j++) {  
  28.             if (tele.length() == 3) tele += '-';  
  29.             if (input[j] >= '0'&&input[j] <= '9')  
  30.                 tele += input[j];  
  31.             if (input[j] >= 'A'&&input[j] <= 'Z')  
  32.                 tele += convert(input[j]);  
  33.         }  
  34.         if (count_telep.count(tele) == 0)  
  35.             count_telep[tele] = 0;  
  36.         count_telep[tele] += 1;  
  37.     }  
  38.     map<string, int>::iterator iter;  
  39.     bool flag = false;  
  40.     for (iter = count_telep.begin(); iter != count_telep.end(); iter++) {  
  41.         if (iter->second > 1) {  
  42.             cout << iter->first << ' ' << iter->second<< endl;  
  43.             flag = true;  
  44.         }  
  45.     }  
  46.     if (!flag)  
  47.         cout << "No duplicates. ";  
  48.     return 0;  
  49. }

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

遍历map的两种方式:

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

map<int,string> mapsfor(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;}