閱讀904 返回首頁    go 阿裏雲 go 技術社區[雲棲]


九度題目1029:魔咒詞典

題目1029:魔咒詞典
時間限製:5 秒
內存限製:32 兆
特殊判題:否
提交:3848
解決:1111
題目描述:
    哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有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?
來源:
2008年浙江大學計算機及軟件工程研究生機試真題

 

 

 

學習使用了C++的map容器,不能c和c++再混了,這樣會出現莫名其妙的編譯錯誤(提交CE)
AC代碼:

#include<stdio.h>
#include<algorithm>
#include<map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
char a[200],str1[100],str2[100],str3[100];
map<string,string>m1;
map<string,string>::iterator iter;//迭代器 
int main()
{
   int i,j,n,x,y,z;
   gets(a);
   while(a[0]!='@')
   {    
      n=strlen(a);
      x=0;y=0;z=0;
      for(i=0;i<n;i++)
      {
         if(a[i]!='['&&a[i]!=']')
         str3[z++]=a[i];
         if(a[i]==']')
         {
            str1[x++]=a[i];
            i+=2;
            break;
         }
         str1[x++]=a[i];
      }
      for(;i<n;i++)
      {
         str2[y++]=a[i];
      }
      m1.insert(pair<string,string>(str1,str2));
      m1.insert(pair<string,string>(str2,str3));
      memset(str1,0,sizeof(str1));
      memset(str2,0,sizeof(str2));
      memset(str3,0,sizeof(str3));
      memset(a,0,sizeof(a));
      gets(a);
   }
   scanf("%d",&n);
   getchar();
   while(n--)
   {
      memset(a,0,sizeof(a));
      gets(a);   
      iter=m1.find(a);
      if(iter==m1.end())
                cout<<"what?\n";
      else cout<<iter->second<<endl;   
   }   
   return 0;   
}

最後更新:2017-04-03 05:39:34

  上一篇:go 八大排序算法總結
  下一篇:go c#對象初始化器、自動實現字段