Description
Input
Output
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char dict[100010][2][15];
int n;
bool isblank(char s[])
{
int k=strlen(s);
while(--k>=0)
if(s[k]>='a'&&s[k]<='z')
return false;
return true;
}
void swap(char a[],char b[])
{
char temp[15];
strcpy(temp,a);
strcpy(a,b);
strcpy(b,temp);
}
void sort(int a,int b)
{
if(a>=b)
return ;
int mid;
char t[15];
strcpy(t,dict[(a+b)/2][1]);
int i,j;
i=a-1;
j=b+1;
do
{
do
++i;
while(strcmp(t,dict[i][1])>0);
do
--j;
while(strcmp(t,dict[j][1])<0);
if(i<j)
{
swap(dict[i][1],dict[j][1]);
swap(dict[i][0],dict[j][0]);
}
}while(i<j);
sort(a,j);
sort(j+1,b);
}
int find(char t[])
{
int mid,le,ri;
le=0;ri=n;
while(le+1<ri)
{
mid=(le+ri)/2;
if(strcmp(dict[mid][1],t)<=0)
le=mid;
else
ri=mid;
}
if(strcmp(dict[le][1],t))
return -1;
return le;
}
int main()
{
char s[30];
n=0;
gets(s);
while(!isblank(s))
{
//strcpy(dict[n][0],s);
sscanf(s, "%s%s", dict[n][0], dict[n][1]);
// if((dict[n][0]))
// break;
++n;
gets(s);
}
//sort(0,n-1,dict);
int min;
sort(0,n-1);
while(scanf("%s", s)!=EOF)
{
int k=find(s);
if(k<0)
printf("%s","eh");
else
printf("%s",dict[k][0]);
printf("\n");
}
return 0;
}