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


HDU1880-魔咒詞典

魔咒詞典
Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9485    Accepted Submission(s): 2431


Problem Description
哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有100000種不同的魔咒,哈利很難全部記住,但是為了對抗強敵,他必須在危急時刻能夠調用任何一個需要的魔咒,所以他需要你的幫助。

給你一部魔咒詞典。當哈利聽到一個魔咒時,你的程序必須告訴他那個魔咒的功能;當哈利需要某個功能但不知道該用什麼魔咒時,你的程序要替他找到相應的魔咒。如果他要的魔咒不在詞典中,就輸出“what?”
 

Input
首先列出詞典中不超過100000條不同的魔咒詞條,每條格式為:

[魔咒] 對應功能

其中“魔咒”和“對應功能”分別為長度不超過20和80的字符串,字符串中保證不包含字符“[”和“]”,且“]”和後麵的字符串之間有且僅有一個空格。詞典最後一行以“@END@”結束,這一行不屬於詞典中的詞條。
詞典之後的一行包含正整數N(<=1000),隨後是N個測試用例。每個測試用例占一行,或者給出“[魔咒]”,或者給出“對應功能”。
 

Output
每個測試用例的輸出占一行,輸出魔咒對應的功能,或者功能對應的魔咒。如果魔咒不在詞典中,就輸出“what?”
 

Sample Input
[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
 

Sample Output
light the wand
accio
what?
what?
 

Author
ZJU
 

Source
浙大計算機研究生複試上機考試-2008年
 

 

 

//用java超內存沒有AC,但是知道這個HashMap怎麼用了,下麵代碼完全能夠解決此類問題

//記錄一下

import java.util.HashMap;
import java.util.Scanner;

public class Main{
 public static void main(String[] args) {
  Scanner input=new Scanner(System.in);
  String s;
  HashMap<String,String> map=new HashMap<String, String>();
  while(!(s=input.nextLine()).equals("@END@")){
   int pos=s.indexOf(' ');
   String Frist=s.substring(0, pos);
   String Second=s.substring(pos+1, s.length());
   map.put(Frist, Second);
   int num=s.indexOf(']');
   String name=s.substring(1, num);
   map.put(Second, name);
  }
  int n=input.nextInt();
  s=input.nextLine();
  while(n-->0){
   s=input.nextLine();
   if(map.get(s)!=null)
    System.out.println(map.get(s));
   else
    System.out.println("what?");
  }
 }
}

最後更新:2017-04-03 12:56:23

  上一篇:go 英語,貴在堅持!
  下一篇:go Xcode5 支持 iOS5 問題解決過程記錄