int bin_search(int front, int rear, string &target) { int mid; while (front <= rear) { mid = (front + rear) / 2; if (dictionary[mid].word == target) return mid; if (dictionary[mid].word > target) rear--; else front++; } return -1; }
int main(int argc, char *argv[]) { int n, i, m, index; ST_PAIR pair; string english, chinese; while (EOF != scanf("%d", &n)) { for (i=0 ; i { cin >> english >> chinese; pair.word = english; storage.push_back(chinese); pair.index = i; dictionary.push_back(pair); } sort(dictionary.begin(), dictionary.end(), cmp()); vector::iterator iter; vector::iterator end = dictionary.end(); /* search */ cin >> m; for (i=0 ; i { cin >> english; index = bin_search(0, n, english); if (-1 == index) cout << "I can't find out this word" << endl; else cout << storage[index] << endl; } dictionary.clear(); storage.clear(); } }