#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string getFileData()
{
string data;
data += "6\n";
data += "b\n"; data += "Second Letter in the Alphabet (Rhymes with 'Bee')\n";
data += "a\n"; data += "The first letter in the alphabet (Vowel)\n";
data += "why\n"; data += "Why what?\n";
data += "why?\n"; data += "Why what?\n";
data += "hello\n"; data += "Hello!\n";
data += "hi\n"; data += "Hello!\n";
return data;
}
struct MyPairOfStrings
{
string first;
string second;
};
bool operator < (const MyPairOfStrings & p1, const MyPairOfStrings & p2)
{
return p1.first < p2.first;
}
int binarySearch(MyPairOfStrings * A, string key, int imin, int imax)
{
if (imax < imin) return -1;
int imid = (imin + imax) / 2;
if (A[imid].first > key)
return binarySearch(A, key, imin, imid - 1);
else if (A[imid].first < key)
return binarySearch(A, key, imid + 1, imax);
else return imid;
}
void siftDown(MyPairOfStrings * array, int start, int end)
{
int root = start;
while (2 * root + 1 <= end)
{
int child = 2 * root + 1;
int swap_with = root;
if (array[swap_with] < array[child])
swap_with = child;
if (child + 1 <= end && array[swap_with] < array[child + 1])
swap_with = child + 1;
if (root != swap_with)
{
MyPairOfStrings temp = array[swap_with];
array[swap_with] = array[root];
array[root] = temp;
root = swap_with;
}
else return;
}
}
void heapify(MyPairOfStrings * array, int size)
{
int start = (size - 2) / 2;
while (start >= 0)
{
siftDown(array, start, size - 1);
start --;
}
}
void heapsort(MyPairOfStrings * array, int size)
{
heapify(array, size);
int end = size - 1;
while (end > 0)
{
MyPairOfStrings temp = array[0];
array[0] = array[end];
array[end] = temp;
end --;
siftDown(array, 0, end);
}
}
int main()
{
// let's pretend this is our file
stringstream file(getFileData());
int n;
file >> n; // get number of entries
file.get(); // ignore newline
MyPairOfStrings * dataArr = new MyPairOfStrings[n];
for (int i = 0; i < n; ++ i)
{
getline(file, dataArr[i].first);
getline(file, dataArr[i].second);
}
heapsort(dataArr, n); // sort the data in
// order to be able to perform binary search
string input;
while (true)
{
getline(cin, input);
if (input.empty()) break; // empty line == quit
for (int i = 0; i < input.length(); ++ i)
if (input[i] >= 'A' && input[i] <= 'Z')
input[i] += 'a' - 'A';
// search the array for the entered string
int i = binarySearch(dataArr, input, 0, n - 1);
// if it's found, print the appropriate message
if (i != -1) cout << dataArr[i].second << endl;
// otherwise, print an error message
else cout << "Sorry, I don't understand..." << endl;
}
delete[] dataArr;
}