1001 487-3279

一个模拟题,需要hash和sort一下

这代码在sdutoj上过了在poj上TLE

感觉时间复杂度没有问题,就是排序的nlogn

有一个很坑的地方No duplicates.

最后那个"."必须输出

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
#include<ctime>
#include<cstring>
#include<stdlib.h>
#include<map>
using namespace std;
int n,i,flag[9999999],tel[100010],sum,x,p,count_;
string t;
struct jgt
{
	int bh;
	string s;
	int operator <(jgt a)const{ return bh<a.bh;}
}dl[100010];
char c;
int hash(char c)
{
	int num;
	num = int(c-'A');
	if(c <= 'O') return 2 + num/3;
	if(c >= 'P'&&c <= 'S') return 7;
	if(c >= 'T'&&c <= 'V') return 8;
	return 9;
}
int main()
{
	scanf("%d",&n);
	for(i = 1;i <= n;i ++)
	{
		int tel_num = 0;
		sum = 0;
		t.clear();
		while(tel_num != 7)
		{
			scanf("%c",&c);
			if(c == '-'||c == '\n') continue;
			else 
			{
				tel_num ++;
				if(c >= 'A' && c<= 'Z') x = hash(c);
				else x = c-'0';
				t = t+char(x+'0');
				sum = sum*10+x;
			}
		}
		flag[sum] ++;
		if(flag[sum] == 2)
		{
			count_ ++;
			dl[count_].bh = sum;
			dl[count_].s = t;
		}
	}
	sort(dl+1,dl+count_+1);
	p = 0;
	for(i = 1;i <= count_;i ++)
	{
			p = 1;
			int j;
			for(j = 0;j <= 6;j ++)
			{
				printf("%c",dl[i].s[j]);
				if(j == 2) printf("-");
			}
			printf(" %d\n",flag[dl[i].bh]);
	}
	if(!p)	printf("No duplicates.");	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_30358129/article/details/76374851