[算法]POJ-ACM 487-3279

http://poj.org/problem?id=1002

Description

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10. 

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: 

A, B, and C map to 2 
D, E, and F map to 3 
G, H, and I map to 4 
J, K, and L map to 5 
M, N, and O map to 6 
P, R, and S map to 7 
T, U, and V map to 8 
W, X, and Y map to 9 

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. 

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) 

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. 

Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. 

Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: 

No duplicates. 温馨提示,记得加句号“.”

Sample Input

12

4873279

ITS-EASY

888-4567

3-10-10-10

888-GLOP

TUT-GLOP

967-11-11

310-GINO

F101010

888-1200

-4-8-7-3-2-7-9-

487-3279

Sample Output

310-1010 2

487-3279 4

888-4567 3

解决方法,我的解决方法就是不断地运用STL标准库里面的类,函数,自己就写了一个字符串到整型,整型到字符串的两个函数,其他都是运用了STL里面的
string,用来存储字符串号码,如ITS-EASY,888-4567
set用来存储整型号码,如8884567,set是红黑树结果,有序插入是对数级别时间,而且insert之后会有返回值标志是否插入成功,当插入不成功时表示set里面已经有了相同的元素,一旦插入失败表示重复,然后就重复的插入到vector 定义的duplicate中。然后里面algorithm中的sort函数进行字典序排序。
然后遍历duplicate输出,输出时转化为号码的标准格式。
#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <algorithm>

using namespace std;

unsigned int str2int(const string& str)

{

    unsigned n = 0;

    unsigned int i = 0;

    if(!str.empty())

    {

        while(i < str.length())

        {

            

            if(str.at(i)>='0'&&str.at(i)<='9')

            {

                n *= 10;

                n += str.at(i)-'0';

            }

            else if (str[i]>='A'&&str[i]<='P')

            {

                n *= 10;

                n += ((str.at(i)-'A')/3+2);

            }

            else if (str[i]>='R'&&str[i]<='Y')

            {

                n *= 10;

                n += ((str.at(i)-'Q')/3+7);

            }

            else

            {

                ;

            }

            ++i;

        }

    }

    return n;

}

void int2str(unsigned int n, string& str)

{

    str.clear();

    unsigned int times = 1000000;

    while(times >= 1)

    {

        if(times==1000)

        {

            str += '-';

        }

        str += (n / times + '0');

        n %= times;

        times /= 10;

    }

    

}



int main()

{

    unsigned int n,phoneNumInt;

    string phoneNumStr;

    set<unsigned int> s;

    pair<set<unsigned int>::iterator,bool> flag;

    vector<unsigned int> duplicate;

    cin>>n;

    while(0 != n)

    {

        cin>>phoneNumStr;

        phoneNumInt = str2int(phoneNumStr);

        flag = s.insert(phoneNumInt);

        if(!flag.second)

        {

            duplicate.push_back(phoneNumInt);

        }

        --n;

    }

    if(duplicate.empty())

    {

        cout<<"No duplicates."<<endl;

    }

    else

    {

        sort(duplicate.begin(),duplicate.end());

        unsigned int i = 0, j = 0;

        while(j < duplicate.size())

        {

            while(j < duplicate.size() && duplicate.at(j)==duplicate.at(i))

            {

                ++j;

            }

            int2str(duplicate.at(i),phoneNumStr);

            cout<<phoneNumStr<<" "<<j-i+1<<endl;

            i = j;

        }

        

    }

    return 0;

} 

 


本文基于知识共享署名-非商业性使用 3.0 许可协议进行许可。欢迎转载、演绎,但是必须保留本文的署名林羽飞扬,若需咨询,请给我发信

你可能感兴趣的:(ACM)