Babelfish
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay Sample Output cat eh loops Hint Huge input and output,scanf and printf are recommended.
Source |
hash, 字符串读取是关键,原来超时,后来参考了别人ac的,改了以下输入输出。
tle:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; typedef struct Entry { char eng[11]; char other[11]; struct Entry * next; }Entry,*Eptr; const int SIZE = 100003; Eptr hash[SIZE]; inline int calcKey(char word[]) { int key = 0; int K = strlen(word); for(int j=0; j<K; ++j) key=((key<<2)+(((int)word[j])>>4))^(((int)word[j])<<10); key = key % SIZE; key = key < 0 ? key + SIZE : key; return key; } int main() { char ch; int key; int ch_no; char temp[11]; Eptr tptr; bool isfind; memset(hash,0,sizeof(hash)); while(scanf("%c",&ch) && ch!='\n') { tptr=(Eptr)malloc(sizeof(Entry)); ch_no=0; while(ch!=' ') { tptr->eng[ch_no]=ch; scanf("%c",&ch); ch_no++; } tptr->eng[ch_no]='\0'; scanf("%c",&ch); ch_no=0; while(ch!='\n') { tptr->other[ch_no]=ch; scanf("%c",&ch); ch_no++; } tptr->other[ch_no]='\0'; key=calcKey(tptr->other); tptr->next=hash[key]; hash[key]=tptr; // cout<<key<<endl; } while(scanf("%c",&ch)!=EOF) { isfind=false; ch_no=0; while(ch!='\n') { temp[ch_no]=ch; ch_no++; scanf("%c",&ch); } temp[ch_no]='\0'; key=calcKey(temp); tptr=hash[key]; while(tptr) { if(!strcmp(tptr->other,temp)) { ch_no=0; isfind=true; while(tptr->eng[ch_no]!='\0') { printf("%c",tptr->eng[ch_no]); ch_no++; } printf("\n"); } tptr=tptr->next; } if(!isfind) { printf("eh\n"); } } return 0; }
该后的ac代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; typedef struct Entry { char eng[12]; char other[12]; struct Entry * next; }Entry,*Eptr; const int SIZE = 100003; Eptr hash[SIZE]; inline int calcKey(char word[]) { int key = 0; int K = strlen(word); for(int j=0; j<K; ++j) key=((key<<2)+(((int)word[j])>>4))^(((int)word[j])<<10); key = key % SIZE; key = key < 0 ? key + SIZE : key; return key; } int main() { int key; char temp[30]; Eptr tptr; bool isfind; memset(hash,0,sizeof(hash)); while(gets(temp)&&temp[0]!=0) { tptr=(Eptr)malloc(sizeof(Entry)); sscanf(temp,"%s %s",tptr->eng, tptr->other); //从已知字符串读出字符串 key=calcKey(tptr->other); tptr->next=hash[key]; hash[key]=tptr; // cout<<key<<endl; } while(gets(temp)&&temp[0]!=0) { isfind=false; key=calcKey(temp); tptr=hash[key]; while(tptr) { if(!strcmp(tptr->other,temp)) { isfind=true; printf("%s\n",tptr->eng); } tptr=tptr->next; } if(!isfind) { printf("eh\n"); } } return 0; }