#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
/* 把输入的每一行的编码解析为拼音和汉字 */
void parse_raw(string &str, string &hanzi, string &pinyin)
{
const char *p = str.c_str();
const char *q = p;
while (*q != '_')
q++;
// note [) 范围是前开后闭
string _hanzi(p, q);
string _pinyin(q + 1, p + str.size());
hanzi = _hanzi;
pinyin = _pinyin;
}
/* 把输入的拼音解析为拼音和数字 */
void parse_input(string &str, string &pinyin, int &nums)
{
const char *p = str.c_str();
const char *q = p;
while (*q != '\0' && !('0' <= *q && *q <= '9'))
q++;
string _pinyin(p, q);
pinyin = _pinyin;
if ('0' <= *q && *q <= '9')
nums = *q - '0';
else
nums = 0;
}
int main()
{
map<string, vector<string> > ssmap;
string str_raw;
/* 定义常量 */
const string tag_start = "Code";
const string tag_search = "Search";
const string tag_end = "End";
string hanzi, pinyin;
int num;
cin >> str_raw;
/* 等于开始信号 */
if (str_raw == tag_start)
{
/* 读取编码表 */
cin >> str_raw;
while (str_raw != tag_search)
{
parse_raw(str_raw, hanzi, pinyin);
ssmap[pinyin].push_back(hanzi);
cin >> str_raw;
}
/* 输入的search */
cin >> str_raw;
/* 解析输入的拼音 */
parse_input(str_raw, pinyin, num);
cin >> str_raw;
/* 结束标志位 */
if (str_raw == tag_end)
{
/* 判断 这个拼音是否存在*/
if (ssmap.find(pinyin) == ssmap.end())
{
cout << "Erro" << endl;
}
else if (num > 0)
{
/* 判断num是否存在 */
if(num < ssmap[pinyin].size())
{
cout << ssmap[pinyin][num - 1] << endl;
}
else
{
cout << "Erro" << endl;
}
}
else
{
/* 输出前5个 */
size_t i = 0;
for ( i = 0; i < ssmap[pinyin].size() - 1 && i != 4; i++)
cout << ssmap[pinyin][i] << ";";
cout << ssmap[pinyin][i] << endl;
}
}
}
return 0;
}