POJ

来源:互联网 发布:js 复制到粘贴板 编辑:程序博客网 时间:2024/04/30 04:28
题目大意:

给你个字典,就是一个字符串对应一个外国话字符串,最多100000条。然后就是要查询最多100000条字符串分别查到它们对应的外国话字符串。(每个字符串长度都超过10)

哈希表就是可以快速查找的,然后网上看到别人用map,瞬间感觉好简单好简单。。。。

#include<iostream>#include<map>using namespace std;#include<string>map<string,string>mp;int main(){char a[30],b[15],c[15];while(gets(a)&&a[0]!='\0'){sscanf(a,"%s %s",&b,&c);mp[c]=b;}char s[15];while(gets(s)&&s[0]!='\0'){if(mp[s][0]=='\0')cout<<"eh\n";else cout<<mp[s]<<endl;}return 0;}
下面是我各种改之后好不容易过的代码:

#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#define N 105000#define MOD 100000using namespace std;char a[N][15]={0};char b[N][15]={0};int hash[N]={0};int next[N]={0};int n=1;bool same(char x[],char y[]){int i=0;while(x[i]!='\0'&&y[i]!='\0'){if(x[i]!=y[i])return 0;i++;}return 1;}int main(){char l[25]={0};while(gets(l)&&l[0]!='\0'){sscanf(l,"%s %s",&a[n],&b[n]);int x=0;int i=0;while(b[n][i]!='\0'){x=x+b[n][i];i++;}x=x%MOD+1;next[n]=hash[x];hash[x]=n;n++;}//ceshi();//ceshi2();char x[15]={0};while(gets(x)&&x[0]!='\0'){int s=0;int i=0;while(x[i]!='\0'){s=s+x[i];i++;}s=s%MOD+1;s=hash[s];bool flag=0;while(s){//cout<<"@";if(same(b[s],x)){flag=1;printf("%s\n",a[s]);break;}s=next[s];}if(flag==0)printf("eh\n");}}

伤心中······

0 0
原创粉丝点击