二分嘛,还是 wata 版本的。
只要二分查找字符应该出现的位置就好了。就是查字典
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <assert.h>
#include <string>
using namespace std;
struct P
{
char e[30];
char f[30];
bool operator < (const P &o) const
{
if(strcmp(f,o.f)>=0)
return 0;
return 1;
}
}dic[200000];
void mySubstr(char* f,char* t,int s,int e)
{
for(int i=0;i<=e-s;i++)
t[i]=f[s+i];
t[e+1]='\0';
}
int cnt;
int main()
{
freopen("acm.in","r",stdin);
char t[50];
while(gets(t))
{
cnt=0;
do
{
int pos=0;
while(t[pos]!=' ')
pos++;
mySubstr(t,dic[cnt].e,0,pos-1);
mySubstr(t,dic[cnt].f,pos+1,strlen(t)-1);
cnt++;
}while(gets(t)&&t[0]!='\0');
sort(dic,dic+cnt);
while(gets(t))
{
int lb=0,ub=cnt;
while(ub-lb>1)
{
int m=(lb+ub)>>1;
if(strcmp(dic[m].f,t)>0)
ub=m;
else
lb=m;
}
if(strcmp(dic[lb].f,t)==0)
printf("%s\n",dic[lb].e);
else
puts("eh");
}
}
return 0;
}