★问题描述
录入 n 个学生的成绩,并查询。
★数据输入
第一行输入包括 n、 m(1<=n<=50,000,1<=m<=100,000)两个数字。
接下来 n 行,每行包含名字和成绩,名字用字符串表示,长度不超过 4.成绩为不
超过 100 的非负整数,名字仅由小写字母组成。
接下来 m 行,每行包括一个名字。
★数据输出
输出 m 行,如果查询的学生不存在,输出”not,exist!”,否则输出学生的成绩。
输入示例 | 输出示例 |
---|---|
3 4 hbb 90 vayh 80 loog 85 leaf loog vayh vyah |
not,exist! 85 80 not,exist! |
★提示
n 个名字各不相同
60%的数据
1<=n,m<=2,000
100%的数据
1<=n<=50,000,1<=m<=100,000
★思路
注意到 名字为不超过4的字符串
将名字中的字符转换为数字,a为1,b为2......,若长度不足4,则补0
这样每个名字都可以与四个数字abcd对应开四维数组arr[27][27][27][27],用arr[a][b][c][d]存成绩。
★Code
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int arr[27][27][27][27]={0};
void set(char*p,int value)
{
int a=0,b=0,c=0,d=0;
if(p[0]!=' ')a=p[0]-96;
if(p[1]!=' ')b=p[1]-96;
if(p[2]!=' ')c=p[2]-96;
if(p[3]!=' ')d=p[3]-96;
arr[a][b][c][d]=value;
}
int get(char*p)
{
int a=0,b=0,c=0,d=0;
if(p[0]!=' ')a=p[0]-96;
if(p[1]!=' ')b=p[1]-96;
if(p[2]!=' ')c=p[2]-96;
if(p[3]!=' ')d=p[3]-96;
return arr[a][b][c][d];
}
int main()
{
char str[4];
int n=0;
int m=0;
int i=0;
int temp=0;
memset(str,' ',sizeof(str));
memset(arr,-1,sizeof(arr));
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",str);
scanf("%d",&temp);
set(str,temp);
memset(str,' ',sizeof(str));
temp=0;
}
for(i=0;i<m;i++)
{
scanf("%s",str);
if(get(str)==-1)
printf("not,exist!
");
else
printf("%d
",get(str));
memset(str,' ',sizeof(str));
}
return 0;
}