poj 1002 487-3279

现在开始刷poj的题,但是这题是有一次学校的contest上考过的,但是那次由于超时的问题,就没有A掉,这次刚敲完,还是受到了上次的影响还是超时,这时候实在想不明白就应该看discuss了,北大的discuss还是灰常给力,起码比起自己的Oj给力多了,自己oj基本没人讨论。。。后来发现自己用的优先队列是问题关键所在,对于这么大的一个储存数组,超时是难免的。后来还是得用纯数组来储存,节省比较时间。

第一个代码是TLE的错误代码,也就是我纠结很久的代码:

#include<cstdio>
#include<string>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	string num;
	int re;
	friend bool operator <(node a,node b)
	{
		return a.num>b.num;
	}
}qe[100005];
int main(void)
{
	int n;
	map<char,char> ma;
	char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y'};
	char b[]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','8','8','8','9','9','9'};
	for(int i=0;i<24;i++)
		ma.insert(make_pair(a[i],b[i]));
	cin>>n;
		priority_queue<node> qq;
		int len=0;
		for(int i=0;i<n;i++)
		{
			string s;
			cin>>s;
			int x=0;
			string y;
			y="00000000";
			y[3]='-';
			for(int e=0;s[e]!='\0';e++)
			{
				if(s[e]=='-')
					continue;
				else if(s[e]>='A'&&s[e]<='Z')
				{
					y[x++]=ma[s[e]];
				}
				else
				{
					y[x++]=s[e];
				}
				if(x==3)
					x++;
			}
			int flag=0;
			for(int j=0;j<len;j++)
			{
				if(qe[j].num==y)
				{
					qe[j].re++;
					flag=1;
					break;
				}
			}
			if(!flag)
			{
				qe[len].num=y;
				qe[len++].re=1;
			}
		}
		for(int i=0;i<len;i++)
		{
			if(qe[i].re>=2)
			{
				qq.push(qe[i]);
			}
		}
		while(!qq.empty())
		{
			node xx=qq.top();
			qq.pop();
			cout<<xx.num<<' '<<xx.re<<endl;
		}
}


下面是看北大discuss大牛的回答后,改写的:

AC代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
using namespace std;
int ar[10000000];
int main(void)
{
	int n;
	map<char,char> ma;
	char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y'};
	char b[]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','8','8','8','9','9','9'};
	for(int i=0;i<24;i++)
		ma.insert(make_pair(a[i],b[i]));
	cin>>n;
	memset(ar,0,sizeof(ar));
	int Min=100000000;
	int Max=-1;
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		int x=0;
		int sum=0;
		for(int e=0;s[e]!='\0';e++)
		{
			if(s[e]=='-')
				continue;
			else if(s[e]>='A'&&s[e]<='Z')
			{
				sum=sum*10+ma[s[e]]-'0';
			}
			else if(s[e]>='0'&&s[e]<='9')
			{
				sum=sum*10+s[e]-'0';
			}
		}
		ar[sum]++;
		int flag=0;
		if(sum>Max)
			Max=sum;
		if(sum<Min)
			Min=sum;
	}
	int flag=0;
	for(int i=Min;i<=Max;i++)
	{
		if(ar[i]>=2)
		{
			printf("%03d-%04d %d\n",i/10000,i%10000,ar[i]);
			flag=1;
		}
	}
	if(!flag)
		printf("No duplicates.\n");
}


 

你可能感兴趣的:(poj 1002 487-3279)