Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 34816 | Accepted: 14908 |
Description
Input
Output
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Source
#include<map> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAX = 27 ; const int MAXN = 100000 + 5 ; map<string,int>m; int num = 0 , len; char s[MAXN][MAX],str[MAXN]; int main(){ //freopen("liuchu.txt","r",stdin); while(gets(s[num])&&s[num][0]!='\0'){ len = strlen(s[num]); int first = 0, k = 0; for(int i=0;i<len;i++){ if(first) str[k++]=s[num][i]; else if(s[num][i]==' '){ first = 1; s[num][i]='\0'; } } m[str]=num++; memset(str,'\0',sizeof(str)); } while(scanf("%s",str)!=EOF){ if(m.count(str)) printf("%s\n",s[m[str]]); else printf("eh\n"); } return 0; }
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int MAX = 27 ; const int MAXN = 100000 + 5 ; struct trie{ int point; trie *next[MAX]; }; int number = 0 ; char s[MAXN][MAX],str[MAX]; trie *root=new trie; void createTrie(char *s,int n){ trie *p=root,*q; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL){ q=new trie; for(int j=0;j<MAX;j++) q->next[j]=NULL; p->next[pos]=q; p=p->next[pos]; } else{ p=p->next[pos]; } } p->point=n; } int findTrie(char *s){ trie *p=root; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL) return -1; p=p->next[pos]; } return p->point; } void delTrie(trie *Root){ for(int i=0;i<MAX;i++){ if(Root->next[i]!=NULL) delTrie(Root->next[i]); } free(Root); } int main(){ //freopen("liuchu.txt","r",stdin); for(int i=0;i<MAX;i++) root->next[i]=NULL; while(gets(s[number])&&s[number][0]!='\0'){ int len=strlen(s[number]),k=0 ; bool judge=false; for(int i=0;i<len;i++){ if(judge) str[k++]=s[number][i]; if(s[number][i]==' '){ s[number][i]='\0'; judge=true; } } createTrie(str,number); number++; } while(scanf("%s",str)!=EOF){ int pose=findTrie(str); if(pose==-1) printf("eh\n"); else printf("%s\n",s[pose]); } delTrie(root); return 0; }
POJ_2503_Babelfish(map or 字典树)
原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/44736013