哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有100000種不同的魔咒,哈利很難全部記住,但是爲了對抗強敵,他必須在危急時刻能夠調用任何一個需要的魔咒,所以他需要你的幫助。
給你一部魔咒詞典。當哈利聽到一個魔咒時,你的程序必須告訴他那個魔咒的功能;當哈利需要某個功能但不知道該用什麼魔咒時,你的程序要替他找到相應的魔咒。如果他要的魔咒不在詞典中,就輸出“what?”
輸入:
首先列出詞典中不超過100000條不同的魔咒詞條,每條格式爲:
[魔咒] 對應功能
其中“魔咒”和“對應功能”分別爲長度不超過20和80的字符串,字符串中保證不包含字符“[”和“]”,且“]”和後面的字符串之間有且僅有一個空格。詞典最後一行以“@END@”結束,這一行不屬於詞典中的詞條。
詞典之後的一行包含正整數N(<=1000),隨後是N個測試用例。每個測試用例佔一行,或者給出“[魔咒]”,或者給出“對應功能”。
輸出:
每個測試用例的輸出佔一行,輸出魔咒對應的功能,或者功能對應的魔咒。如果魔咒不在詞典中,就輸出“what?”
樣例輸入:
[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky
樣例輸出:
light the wand
accio
what?
what?
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
class FindString
{
private:
string m_str;
public:
FindString(const string &str):m_str(str){}
bool operator()(const pair<string,string> &p) const
{
return m_str == p.second;
}
};
int main()
{
string a;
typedef map<string,string> StringMap;
typedef StringMap::iterator StringMapIter;
StringMap smap;
StringMapIter it;
while(getline(cin,a))
{
if(a == "@END@")
break;
string::size_type pos = a.find(']');
string first = a.substr(1,pos - 1);
string second = a.substr(pos + 2);
smap.insert(make_pair(first,second));
}
int n;
cin >> n;
cin.ignore();
for(int i = 0; i < n; ++i)
{
string c;
getline(cin,c);
if(c[0] == '[')
{
c = c.substr(1,c.size() - 2);
it = smap.find(c);
if(it == smap.end())
{
cout << "what?" << endl;
}
else
{
cout << it->second << endl;
}
}
else
{
it = find_if(smap.begin(),smap.end(),FindString(c));
if(it == smap.end())
{
cout << "what?" << endl;
}
else
{
cout << it->first << endl;
}
}
}
return 0;
}