codevs3004 我是世界之王 ——map或字符串哈希或字典序

本文详细描述了乘客Jack在泰坦尼克号上的经历,以及如何通过房间编号找到自己的位置。通过输入描述,我们了解了房间编号的规则,并利用不同的方法(如哈希表、四进制转换、前缀树)来快速定位房间位置。故事背景与算法应用巧妙结合,展现了历史与现代技术的碰撞。
3004 我是世界之王 


题目描述 Description
Cause we're going to America!Jack赢得了去往美国的船票,一路狂奔上船。这里是泰坦尼克号,空前奢华、永不沉没的泰坦尼克号。1912年4月10日,这艘曾是人类史上建造过的最大最豪华的的两万吨巨轮开始了她的处女航。目的地:美国。270米的庞大身躯在无际的海洋上驰骋,她是当之无愧的时代的骄子。

Jack拿着船票登上泰坦尼克号,却不知这将是一次通向死亡的旅途。因为是三等舱,船票上只标注了房间的英文编号,却没有标注具体位置。同Jack一块的其他三等舱旅客也不知所措。索性船员意识到了这点,将房间编号及其对应的位置张贴了出来。房间的引文编号仅由A、B、C、D四个字母组成,长度从2个到12个字母不等。Jack和其他乘客都想快点知道自己的房间在什么地方。

得知位置后,Jack飞快地放好行李,然后奔向船头。"I can see the Statue of Liberty already(自由女神像)."Jack已经按耐不出自己的心情,仿佛美国就在前方。他爬上桅杆,眺望着远方,"I'm the king of the world! I'm the king of the world!"

而就在此时,一个叫Rose年轻女子的身影定格在了Jack的脑海中。

输入描述 Input Description
第一行有两个整数N、M。N表示旅客的数量,M表示房间的数量。接下来M行,每行为一个字符串和一个整数,分别表示房间的英文编号和房间的具体位置。在接下来N行每行有一个字符串,表示这N位乘客的房间的英文编号。数据保证编号仅由A、B、C、D四个大写字母组成。

输出描述 Output Description
输出文件总共包含N行,按照输入顺序依次输出每位乘客的房间位置。如果此房间不存在,则输出incorrect number。

样例输入 Sample Input
5 6
CA 2
BC 3
ADD 1
BAD 5
CC 4
CA
DA
BAD
BC
CA

ADD

方法一:c++stl提供的map可以直接解决这个问题。不过稍慢。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int n,m,id;
char c[20];
string s;

map<string,int>dir;
int main(){

	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%s%d",c,&id);
		s=c;
		dir[s]=id;
	}
	for(int i=0;i<m;i++){
		scanf("%s",c);
		s=c;
		if(dir.count(s))printf("%d\n",dir[s]);
		else printf("incorrect number\n");
		
	}
	return 0;
}

方法二:只出现四个字符,可以用四进制,最多12为,数据范围为4^12,大约17m,数据范围可以接受,哈希到10^的数据范围,没有统计冲突,ac,侥幸。

比用map快很多。

#include<iostream>
#include<cstdio> 
using namespace std;
const int maxn=10000000+9;
const int prime=10000007;
char s[13];
int h[maxn]={0};
int hash(char s[]){	
	int ans=0,i=0;
	while(s[i]!=0){
	  ans=ans*4+s[i]-'A';
	  i++;
	}
	return ans%prime;		
}
int main(){	 
	int n,m,id,t;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){	
		scanf("%s%d",s,&id);	
		h[hash(s)]=id;
	}
	for(int i=1;i<=m;i++){
		scanf("%s",s);
		t=hash(s);
		if(h[t])printf("%d\n",h[t]);
		else printf("incorrect number\n");
	}
	return 0;
}

方法三,也可以用trie树,用指针,所用空间就是比较大,速度比哈希慢一点,但是map的一半。

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
using namespace std;  
char s[20];  
int N,M,id;
struct node  
{   int val; 
    node *childs[4];  
    node()  
    {   val=0;        
        int i;  
        for(i=0;i<4;i++)  
        childs[i]=NULL;  
    }  
};  
node *root=new node;  
node *current,*newnode;  
void insert(char *str,int value)  
{  
    int i,m,len=strlen(str);  
    current=root;  
    for(i=0;i<len;i++)  
    {  
        m=str[i]-'A';  
        if(current->childs[m]!=NULL)  
            current=current->childs[m];   
        else  
        {  
            newnode=new node;    
            current->childs[m]=newnode;  
            current=newnode;  
        } 
    } 
	current->val=value;
}  
int search(char *str)  
{  
    int i,m,len=strlen(str);  
    current=root;  
    for(i=0;i<len;i++)  
    {  
        m=str[i]-'A';  
        current=current->childs[m];
		if(current==NULL) return 0;
    }  
    return current->val;
}  
int main()  
{  
    scanf("%d%d",&N,&M);
    while(N--){
    	scanf("%s%d",s,&id);
    	insert(s,id);
	}
	while(M--){
		scanf("%s",s);
		int tmp=search(s);
		if(tmp)printf("%d\n",tmp);
		else printf("incorrect number\n");
	}      
    return 0;  
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值