POJ 1002 487-3279

1. 试了各种数据类型保存输入的字符串,比如string,字符数组。在string类型中,不能一个一个字符地拷贝字符串,因为字符串末尾还有一个字符。

2. 字符型数字转换成int类型;

3. 自我感觉change函数写的比较巧妙,跟Curling 2.0有点类似。是把一个数组(有重复元素)无重复的放到另外一个数组。



#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int telnum[100010];
int tel[100010][7];
int n;
struct Node
{
    int ans;
    int num;
} node[100010];
bool cmp(int a, int b)
{
    return a < b;
}
void change(int k, string str)
{
    int i, j = 0;
    for(i = 0; i < 7; i++)
    {

        while(str[j] == '-')
            j++;
        if(str[j] == '0' || str[j] == '1' || str[j] == '2' || str[j] == '3' || str[j] == '4' || str[j] == '5' || str[j] == '6' || str[j] == '7' || str[j] == '8' || str[j] == '9')
        {
            tel[k][i] = str[j] - '0';//操,=写成==了,测试了一个多小时。。。
            j++;
            continue;
        }
        if(str[j] == 'A' || str[j] == 'B' || str[j] == 'C')
        {
            tel[k][i] = 2;
            j++;
            continue;
        }

        if(str[j] == 'D' || str[j] == 'E' || str[j] == 'F')
        {
            tel[k][i] = 3;
            j++;
            continue;
        }
        if(str[j] == 'G' || str[j] == 'H' || str[j] == 'I')
        {
            tel[k][i] = 4;
            j++;
            continue;
        }
        if(str[j] == 'J' || str[j] == 'K' || str[j] == 'L')
        {
            tel[k][i] = 5;
            j++;
            continue;
        }
        if(str[j] == 'M' || str[j] == 'N' || str[j] == 'O')
        {
            tel[k][i] = 6;
            j++;
            continue;
        }
        if(str[j] == 'P' || str[j] == 'R' || str[j] == 'S')
        {
            tel[k][i] = 7;
            j++;
            continue;
        }
        if(str[j] == 'T' || str[j] == 'U' || str[j] == 'V')
        {
            tel[k][i] = 8;
            j++;
            continue;
        }
        if(str[j] == 'W' || str[j] == 'X' || str[j] == 'Y')
        {
            tel[k][i] = 9;
            j++;
            continue;
        }
    }
}
int getnum()
{
    int i, j = 0;
    for(i = 0; j < n; i++)
    {
        node[i].num = 0;
        node[i].ans = telnum[j];
        node[i].num++;
        while(telnum[j] == telnum[j+1])
        {
            j++;
            node[i].num++;
        }
        j++;
    }
    return i;
}

int main()
{
    string str;
    int count = 0;
    cin >> n;
    memset(tel, 0, sizeof(tel));
    for(int i = 0; i < n; i++)
    {
        cin >> str;
        change(i, str);
/*        for(int j = 0; j < 7; j++)
        {
            cout << tel[i][j];
        }
        cout << endl;*/
        telnum[i] = tel[i][0] * 1000000 + tel[i][1] * 100000 + tel[i][2] * 10000 + tel[i][3] * 1000 + tel[i][4] * 100 + tel[i][5] * 10 + tel[i][6];
    }
    sort(telnum, telnum + n, cmp);
    int m = getnum();
    for(int i = 0; i < m; i++)
    {
        if(node[i].num > 1)
        {
            cout << node[i].ans / 10000 << '-' << node[i].ans % 10000 << ' ' << node[i].num << endl;
        }
        else count++;
    }
    if(count == n)
        cout << "No duplicates." << endl;
    return 0;
}


你可能感兴趣的:(poj)