题目描述 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。
数据范围及提示 Data Size & Hint
N,M<=500000
字符串由A、B、C、D四个大写字母组成,长度在2到12位之间。由于输入文件较大,建议使用scanf输入。
一看就是hash水题嘛…..
于是…
WTF….
是不是hash的姿势不对?
去看某fluency的代码….丫搞的hash值贼小…
所以…
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
using namespace std;
typedef unsigned long long ll;
ll hs=17;
ll hash[12000005];
const ll ha=10000007;
ll py[16];
int n,m;
ll get()
{
char s[15];
scanf("%s",s);
int l=strlen(s);
ll ans=0;
for(int i=0;i<l;i++)
ans=ans*4+s[i]-'A';
return ans%ha;
}
int main()
{
srand(time(0));
py[0]=1;
for(int i=1;i<=13;i++)
py[i]=py[i-1]*hs;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
ll x;
ll hah=get();
scanf("%llu",&x);
hah%=ha;
hash[hah]=x;
}
for(int i=1;i<=m;i++)
{
ll hah=get();
hah%=ha;
if(hash[hah])
printf("%llu\n",hash[hah]);
else
puts("incorrect number");
}
return 0;
}
然后还要注意一点….在CODEVS上用scanf(“%s”)读字符串比用getchar()快….虽然不知道为什么。
于是我又用map水了一发…
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<map>
using namespace std;
typedef unsigned long long ll;
int n,mm;
map<string,int>m;
void read(int &a)
{
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
int ans=0;
while(c<='9'&&c>='0')
ans=ans*10+c-'0',c=getchar();
a=ans;
return;
}
int main()
{
read(n),read(mm);
for(int i=1;i<=n;i++)
{
char hah[15];
scanf("%s",hah);
int x;
read(x);
m[hah]=x;
}
for(int i=1;i<=mm;i++)
{
char hah[15];
scanf("%s",hah);
if(m[hah])
printf("%d\n",m[hah]);
else
puts("incorrect number");
}
return 0;
}
不用百分号s过不了系列….
然后就这样了:(上为map,下为hash)
事实证明:STL特别慢!STL特别慢!STL特别慢!重说三,用STL需谨慎
&&这是我hashWA掉的代码…大神求看
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
using namespace std;
typedef unsigned long long ll;
ll hs=17;
ll hash[10000005];
const ll ha=1000007;
ll py[16];
int n,m;
ll get()
{
char c=getchar();
while(c<'A'||c>'D')
c=getchar();
string s="";
ll ans=1;
int tot=0;
while(c<='D'&&c>='A')
{
ans+=ans*((++tot)*hs+(c-'A'+1)*5);
c=getchar();
}
return ans%ha;
}
int main()
{
srand(time(0));
py[0]=1;
for(int i=1;i<=13;i++)
py[i]=py[i-1]*hs;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
ll x;
ll hah=get();
scanf("%llu",&x);
hah%=ha;
while(hash[(hah+51145)%ha])
hah=(hah+51145)%ha;
hash[hah]=x;
}
for(int i=1;i<=m;i++)
{
ll hah=get();
hah%=ha;
if(hash[hah])
printf("%llu\n",hash[hah]);
else
puts("incorrect number");
}
return 0;
}