POJ2503 Babelfish map做法

map是一个关联式容器,用来增删修改比较好用

//memory:10480K
//time:891MS
#include
#include
#include
using namespace std;

mapt;
char s[50],a[15],b[15];
int main()
{
	while(1)
	{
		gets(s);
		if(s[0]=='\n'||s[0]=='\0')break;
		sscanf(s,"%s%s",&a,&b);
		t[b]=a;
	}
	while(~scanf("%s",&a))
	{
		if(t[a].length()==0)
			printf("eh\n");
		else 
			printf("%s\n",t[a].c_str());
	}
	return 0;
}

重载new与delete进行优化

//memory:7904K
//time:469MS
#include
#include
#include
using namespace std;

mapt;
char s[50],a[15],b[15];
char buf[50000000];
int sizet =0;

void* operator new(size_t size){
    void *p = buf + sizet;
    sizet  += size;
    return p;

}
void operator delete(void* p){
}
int main()
{
	while(1)
	{
		gets(s);
		if(s[0]=='\n'||s[0]=='\0')break;
		sscanf(s,"%s%s",&a,&b);
		t[b]=a;
	}
	while(~scanf("%s",&a))
	{
		if(t[a].length()==0)
			printf("eh\n");
		else 
			printf("%s\n",t[a].c_str());
	}
	return 0;
}

可以发现,运行速度快了近一倍!

你可能感兴趣的:(map)