POJ-1002

题意:电话上按键对应着数字。现在给n个电话,求排序。相同的归一类

思路:首先将所有的输入数字串全部转换,然后使用map来表示字符串与出现次数的关系。

新学到的:

  • map<key, value>会自动按照key值从小到大排序。
  • 字符串的插入函数,例如string s,我们可以使用s.insert()来操作,其函数:s.insert(在第几个位置插入,插入元素个数,插入的元素)。

代码:

#include <map>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
const int N = 100010;
string change(string s)
{
    string ans;
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == 'A' || s[i] == 'B' || s[i] == 'C') ans += '2';
        else if(s[i] == 'D' || s[i] == 'E' || s[i] == 'F')  ans += '3';
        else if(s[i] == 'G' || s[i] == 'H' || s[i] == 'I')  ans += '4';
        else if(s[i] == 'J' || s[i] == 'K' || s[i] == 'L')  ans += '5';
        else if(s[i] == 'M' || s[i] == 'N' || s[i] == 'O')  ans += '6';
        else if(s[i] == 'P' || s[i] == 'R' || s[i] == 'S')  ans += '7';
        else if(s[i] == 'T' || s[i] == 'U' || s[i] == 'V')  ans += '8';
        else if(s[i] == 'W' || s[i] == 'X' || s[i] == 'Y')  ans += '9';
        else if(s[i] != '-')ans += s[i];
    }
    return ans;
}

int main()
{   
    int flag = 0;
    ios::sync_with_stdio(false);
    int n;  cin >> n;
    map<string, int> need;
    for(int i = 1; i <= n; i++)
    {
        string s;   cin >> s;
        s = change(s);
        s.insert(3, 1, '-');
        need[s] ++;
    }
    map<string, int>::iterator it;
    for(it = need.begin(); it != need.end(); ++it)
    {
        if(it->second >= 2)
        {
            flag = 1;
            cout << it->first << " " << it->second << endl;
        }
    }
    if(flag == 0) cout << "No duplicates." << endl;
    return 0;
}

 

posted @ 2020-08-31 11:42  ACM-Epoch  阅读(79)  评论(0编辑  收藏  举报