poj 1002 487-3279之map解法


487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 293365 Accepted: 52575
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
Source


  • 分析:给出n个电话号码的不同形式,输出数量大于1的号码和号码数目.
    首先考虑存储问题,肯定用字符串输入,然后每个号码有对应的数目,而且号码要按照字典序有序输出.有序,映射,很明显用map比较简单.

  • 思路:把所有号码插入map中,然后按照要求遍历输出.

 首先看看一些Map的几个基础知识点
/map添入元素
(1)利用pair
map<int,string>ma;
ma.insert(pair<int,int>(2,"LiMing"));
或者ma.insert(make_pair(2,"LiMing"));

(2)利用map的value_type
map<int,string>ma;
ma.insert(map<int,string>::value_type(2,"LiMing"));

(3)利用数组
map<int,string>ma;
ma[2] = "LiMing";

map<string,int>s;
str = "LiMing";
s[str] = 2;
另外,key值相同的时候无法添加到map中;
如果value对应的key值需要更新的时候,yua需要先获取value值,然后删去元素key,value对应元素,再重新插入新key,value对应的元素.
如果key对应的value值需要更新,可以直接赋值或操作,比如s[str] = 3;或者s[str]++;

map删除元素
移除某个map中某个条目用erase()
该成员方法的定义如下
(1)iterator erase(iterator it); //通过一个条目对象删除
(2)iterator erase(iterator first, iterator last);//删除一个范围
(3)size_type erase(const Key& key); //通过关键字删除
(4)map的清空函数clear()就相当于 enumMap.erase(enumMap.begin(), enumMap.end());

//map查找元素
 (1)用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

 (2)用find函数来定位数据出现位置,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器.

(3)map的遍历
map<int,string>ma;
map<int,string>::iterator na;
for (na = ma.begin(); na != ma.end(); na++)
    cout << na->first << " " << na->second << endl;
前序遍历后序遍历都可以
/*
* Filename:    code.cpp
* Created:     2017-07-23
* Author:        wyl6 
*[mail:17744454343@163.com]
* Desciption:  Desciption
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
#include <map>
using namespace std;

#define INT_MAX 1 << 30
#define MAX 100
typedef long long ll;
int n;
char s[2000];
map<string,int> ma;
map<string,int>::iterator na;
int flag = 1;

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

int main()
{
    scanf("%d",&n);
    while (n--){
        string sChange = "";
        scanf("%s",&s);
        for (int i = 0; i < strlen(s); i += 1)
            if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9'))
                sChange += change(s[i]);
        ma[sChange]++;
    }
    for (na = ma.begin(); na != ma.end(); na++){
        if (na->second > 1){
            for (int i = 0; i < 7; i += 1){
                printf("%c",na->first[i]);
                if (i == 2) printf("-");  
            }
            printf(" %d\n",na->second);
            flag = 0;
        }
    }
    if (flag) printf("No duplicates.\n");
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值