JOJ1490:487-3279

传送门:http://acm.jlu.edu.cn/joj/showproblem.php?pid=1490

思路:就是把所有的字符串转换成XXX-XXXX的格式,统计个数大于1的字符串,并按字典序输出,建议用map做,熟悉一下map的操作。

代码:

#include 
#include 
#include 
#include 
using namespace std;

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

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        map mapstr;
        for (int i = 0; i < n; ++i)
        {
            char str1[100], str2[100];
            cin >> str1;
            int cnt = 0;
            for (int l = 0; str1[l]; ++l)
            {
                if (str1[l] == '-') continue;
                str2[cnt++] = change(str1[l]);
                if (cnt == 3)
                    str2[cnt++] = '-';
            }
            str2[cnt] = '\0';
            mapstr[str2]++;
        }
        map :: iterator iter;
        bool flag = false;
        for (iter = mapstr.begin(); iter != mapstr.end(); iter++)
        {
            if (iter -> second != 1)
            {
                flag = true;
                cout << iter -> first << " " << iter -> second << endl;
            }
        }
        if (!flag)
        {
            cout << "No duplicates." << endl;
        }
    }
    return 0;
}


你可能感兴趣的:(字符串,模拟)