POJ 1002 487-3279 模拟

487-3279
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 306663   Accepted: 54796

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

题目大致意思:

根据字母到数字的映射关系,找出重复的号码,并且按照字典序输出,并输出重复次数,若没有重复,输出No duplicates. 

解法一:map G++过不去 C++能过

主要是利用map的迭代器可以省时间

map 可以自定义比较,插入时候就按照自定义顺序比较的

it->first :key

it->second:value

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<cstdlib>
#include<stdio.h>
#include<cmath>
#include<string>
#include<vector> 
#include<algorithm>
using namespace std;
int n;
typedef pair<string,int> PAIR;
//ostream& operator<<(ostream& out,const PAIR &p){
//	if(p.second > 1){
//		return out << p.first <<" " << p.second;
//	}
//	//return out<<p.first << "\t"<<p.second;
//}
map<string,int,less<string> > res; 
vector<string> pre; 
set<string> vis;
bool islet(char c){
	if(c >= 65 && c <= 90 || c >= 97 && c <= 122){
		return true;
	}
	return false;
}
void change(string &str){
	int len = str.size();
	for(int i = 0; i < len; ++i){
		if(isdigit(str[i]) || str[i] == '-'){
			continue;
		}
		else{
			if(str[i] == 'A' || str[i] == 'B' || str[i] == 'C')
				str[i] = '2';
			else if(str[i] == 'D' || str[i] == 'E' || str[i] == 'F')
				str[i] = '3';
			else if(str[i] == 'G' || str[i] == 'H' || str[i] == 'I')
				str[i] = '4';
			else if(str[i] == 'J' || str[i] == 'K' || str[i] == 'L')
				str[i] = '5';
			else if(str[i] == 'M' || str[i] == 'N' || str[i] == 'O')
				str[i] = '6';	
			else if(str[i] == 'P' || str[i] == 'R' || str[i] == 'S')
				str[i] = '7';
			else if(str[i] == 'T' || str[i] == 'U' || str[i] == 'V')
				str[i] = '8';
			else if(str[i] == 'W' || str[i] == 'X' || str[i] == 'Y')
				str[i] = '9';
		}
	}
	++res[str];
} 

int main()
	{
		ios::sync_with_stdio(false);
		string str,t;
		cin >> n;
		while(n--){
			cin >> str;
			int len = str.size();
			t = ""; 
			int cnt = 0;
			for(int i = 0; i < len; ++i){
				if(islet(str[i]) || isdigit(str[i])){
					t += str[i];
					++cnt;
					if(cnt == 3){
						t += "-";
					}
				}
				
			}
			if(t != ""){
				pre.push_back(t);
			}
		}
		for(int i = 0; i < pre.size(); ++i){
		//	cout << "预处理后的数据  " << pre[i] << "len = " << pre[i].size()<<endl;
			change(pre[i]);
		//	cout << "映射后的数据  " << pre[i] << endl;
		}
		
		bool flag = false;
		for(map<string,int>::iterator it = res.begin(); it != res.end(); ++it){
			if(it->second > 1){
				flag = true;
				cout << it->first << " "<<it->second <<endl;
			}
		}
//		sort(pre.begin(),pre.end());
//		for(int i = 0; i < pre.size(); ++i){
//			if(res[pre[i]] > 1 && vis.count(pre[i]) == 0){
//				flag = true;
//				vis.insert(pre[i]);
//				cout << pre[i] << " "<<res[pre[i]] <<endl;
//			}
//		}
		
		if(!flag){
			cout << "No duplicates.\n";
		}
		
		return 0;
	}

方法二:转化为数字串处理:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<cstdlib>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int b[100000+5];

int main()
	{	
		int n;
		scanf("%d",&n);
		getchar();
		int cnt = 0;
		while(n--){
			char a[105];
			gets(a);
			int len = strlen(a);
			int num = 0;
			for(int i = 0; i < len; ++i){
				if(a[i] == '-')
					continue;
				if(a[i] >= '0' && a[i] <='9'){
					num = num*10 + a[i]-'0';
				}
				else if(a[i] <= 'O'){
					num = num*10 + (a[i]-'A')/3+2;
				}
				else if(a[i] == 'P' || a[i] == 'R' || a[i] == 'S'){
					num = num*10 + 7;
				} 
				else if(a[i] == 'T' || a[i] == 'U' || a[i] == 'V'){
					num = num*10 + 8;
				}
				else if(a[i] == 'W' | a[i] == 'X' || a[i] == 'Y'){
					num = num*10 + 9;
				}
			}
			b[cnt++] = num;
		} 
		sort(b,b+cnt);
		bool flag = true;
		for(int i = 1; i < cnt;){
			int num = 1;
			while(b[i-1] == b[i] && i < cnt){
				++num;
				++i;
			}
			if(num > 1){
				flag = false;
				printf("%03d-%04d %d\n",b[i-1]/10000,b[i-1]%10000,num);
			}
			++i;
		}
		if(flag){
			printf("No duplicates.\n");
		}
	
		return 0;
	}


猜你喜欢

转载自blog.csdn.net/tianweidadada/article/details/80543226