hdu 1880 魔咒词典(多种方法)

133 篇文章 18 订阅
75 篇文章 9 订阅
题目描述:
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。

    给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
输入:

    首先列出词典中不超过100000条不同的魔咒词条,每条格式为:

    [魔咒] 对应功能

    其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
    词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。

输出:
    每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
样例输入:
[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky
样例输出:
light the wand
accio
what?
what?
提示:
来源:

2008年浙江大学计算机及软件工程研究生机试真题


//二分查找(625ms)
#include "iostream"
#include "stdio.h"
#include "math.h"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 100010
int cnt;

struct DIC
{
	char a[25];
	char b[85];
}d[2][N];

int comp1(const void *a,const void *b)
{
	struct DIC *A=(DIC*) a;
	struct DIC *B=(DIC*)b;
	return strcmp(A->a,B->a);
}

int comp2(const void *a,const void *b)
{
	struct DIC *A=(DIC*) a;
	struct DIC *B=(DIC*)b;
	return strcmp(A->b,B->b);
}

void Search(char*s, int dicset,int l,int r)
{
	int i;
	int m=(l+r)>>1;
	if(dicset==1)
	{	
		while(l<=r)
		{
			m=(l+r)>>1;
			if(strcmp(s,d[1][m].b)==0)
			{	
				for(i=1;i<strlen(d[1][m].a)-1;i++)
					printf("%c",d[1][m].a[i]);
				cout<<endl;
				return;
			}
			else if(strcmp(s,d[1][m].b)>0)
				l=m+1;
			else 
				r=m-1;
		}
		puts("what?");
		return ;
	}
	else if(dicset==0)
	{	
		while(l<=r)
		{
			m=(l+r)>>1;
			if(strcmp(s,d[0][m].a)==0)
			{
				printf("%s\n",d[0][m].b);
				return ;
			}
			else if(strcmp(s,d[0][m].a)>0)
				l=m+1;
			else 
				r=m-1;
		}
		puts("what?");
		return;
	}
}


int main()
{
	char str1[25],str2[85],str[120];
	int i,j;
	cnt=0;
	while(gets(str))
	{
		if(strcmp(str,"@END@")==0)
			break;
		i=j=0;
		while(str[i-1]!=']')
			str1[j++]=str[i++];
		str1[j]='\0';

		j=0,i++;
		while(i<strlen(str))
			str2[j++]=str[i++];
		str2[j]='\0';

		strcpy(d[1][cnt].a,str1);
		strcpy(d[1][cnt].b,str2);
		strcpy(d[0][cnt].a,str1);
		strcpy(d[0][cnt].b,str2);
		cnt++;
	}
	qsort(d[0],cnt,sizeof(d[0][0]),comp1);
	qsort(d[1],cnt,sizeof(d[1][0]),comp2);

	int n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		int dic_index=1;
		gets(str2);
		if(str2[0]=='[')
			dic_index=0;
		Search(str2,dic_index,0,cnt-1);
	}
}

#include "iostream"
#include "map"
#include"string"
using namespace std;
map<string,string>Dic;
char a[30],b[100];
string a1,b1;
char c[130];

int main()
{
    while (gets(c)&&c[0]!='@')
    {
        int i=1,x=1;
        a[0]='[';
        while (c[i-1]!=']')
        {
            a[x++]=c[i++];
        }
        a[x]='\0';
        i++;
        x=0;
        while (c[i]!='\0')
        {
            b[x++]=c[i++];
        }
        b[x]='\0';
        a1=a;
        b1=b;
        Dic[a1]=b1;
        Dic[b1]=a1;
    }
    int n;
    scanf("%d",&n);
    gets(a);
    while(n--)
    {
        gets(a);
        a1=a;
        map<string,string>::iterator l1=Dic.find(a1);
        if(l1 == Dic.end())
            cout<<"what?"<<endl;
        else
        {
            b1=l1->second;
            if(b1[0]=='[')
                b1=b1.substr(1,b1.length()-2);
            cout<<b1<<endl;
        }
    }
}


//AC-暴力2187ms)
#include "iostream"
#include "stdio.h"
#include "math.h"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 200010
#define M 'z'-'['+10
char dic[2][N][81];
int cnt;

int Search(char* s,int dicset)
{
	int i;
	for(i=0;i<cnt;i++)
		if(strcmp(s,dic[dicset][i])==0)
			return i;
	if(i==cnt)
		return -1;
}

int main()
{
	char str1[21],str2[81],str[120];
	int i,j;
	cnt=0;
	while(gets(str))
	{
		if(strcmp(str,"@END@")==0)
			break;
		i=j=0;
		while(str[i-1]!=']')
			str1[j++]=str[i++];
		str1[j]='\0';

		j=0,i++;
		while(i<strlen(str))
			str2[j++]=str[i++];
		str2[j]='\0';

		strcpy(dic[0][cnt],str1);
		strcpy(dic[1][cnt],str2);
		cnt++;
	}
	int n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		int dic_index=1;
		gets(str2);
		if(str2[0]=='[')
			dic_index=0;
		int index=Search(str2,dic_index);
		if(index==-1)
			puts("what?");
		else
		{
			if(dic_index==0)
				printf("%s\n",dic[1][index]);
			else
			{
				for(i=1;i<strlen(dic[0][index])-1;i++)
					printf("%c",dic[0][index][i]);
				cout<<endl;
			}
		}
	}
}



//MLE CODE

#include "iostream"
#include "stdio.h"
#include "math.h"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 200010
#define M 'z'-'['+10
char dic[N][81];
int cnt;

struct Trie
{
	struct Trie* node[M];
	int index;
	Trie()
	{
		for(int i=0;i<M;i++)
			node[i]=NULL;
		index=-1;
	}
};

void Insert(Trie *root,char* s,char *stodic,int len)
{
	Trie *r=root;
	for(int i=0;i<len;i++)
	{
		int c=s[i]-'[';
		if(s[i]==' ')
			c='z'+1-'[';
		if(r->node[c]==NULL)
			r->node[c]=new Trie;
		r=r->node[c];
	}
	strcpy(dic[cnt],stodic);
	r->index=cnt++;
}

int main()
{
	char str1[21],str2[81];
	int i,j;
	cnt=0;
	Trie *root,*r;
	root=new Trie;
	while(scanf("%s",str1)&&strcmp(str1,"@END@"))
	{
		getchar();
		gets(str2);
		Insert(root,str1,str2,strlen(str1));
		Insert(root,str2,str1,strlen(str2));
	}
	int n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		r=root;
		bool indflag=false;
		gets(str2);
		if(str2[0]=='[')
			indflag=true;
		for(i=0;i<strlen(str2)&&r;i++)
		{
			int c=str2[i]-'[';
			if(str2[i]==' ')
				c='z'+1-'[';
			r=r->node[c];
		}
		if(i<strlen(str2))
			puts("what?");
		else
		{
			if(indflag)
				printf("%s\n",dic[r->index]);
			else
			{
				for(i=1;i<strlen(dic[r->index])-1;i++)
					printf("%c",dic[r->index][i]);
				cout<<endl;
			}
		}
	}
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值