题目:
今天桐桐接到一个任务,就是要把一篇英语文章翻译成中文。
对桐桐来说这任务实在太艰巨了,可怜的桐桐只好拿着英文字典,一句句慢慢翻译起来。
希望桐桐能在规定的时间内完成吧!
输入格式
第一行一个整数N,表示字典中一共有多少单词(N≤20000)。
接下来每两行表示一个单词,其中:
第一行是一个长度≤100的字符串,表示这个单词,全部小写字母,单词不会重复。
第二行是一个整数,表示这个单词在字典中的页码。
接下来一行是一个整数M,表示要查的单词数(M≤10000)。
接下来M行,每行一个字符串,表示要查的单词,保证在字典中存在。
输出格式
M行,每行一个整数,表示第i个单词在字典中的页数。
输入/输出例子1
输入:
2
scan
10
word
15
2
scan
word
输出:
10
15
样例解释
无
代码(C++):
#include<bits/stdc++.h>
using namespace std;
int n;
struct dictionary
{
int ID;
string word;
};
dictionary a[20001];
bool cmp(dictionary x,dictionary y)
{
return x.word<y.word;
}
int find(string s)
{
int L=1,R=n+1,mid;
while(L+1<R)
{
mid=(L+R)/2;
if(a[mid].word<=s) L=mid;
else R=mid;
}
return a[L].ID;
}
int main(){
int Q;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i].word>>a[i].ID;
sort(a+1,a+n+1,cmp);
cin>>Q;
for(int i=1;i<=Q;i++)
{
string s;
cin>>s;
cout<<find(s)<<endl;
}
return 0;
}