PKU2003
http://d.hatena.ne.jp/Ozy/20060727#p1を見てやってみた。
やり方は、まったくもってこれ以上ないくらいごくごく普通の何の変哲もない(略
import java.io.*; import java.util.*; class Main{ public static final String hyphen60 = "------------------------------------------------------------\n"; public static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] argv) throws Exception{ assert hyphen60.length() == 61; BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); Tree t = new Tree(r.readLine(), null); for(String s; (s=r.readLine()) != null; ){ StringTokenizer st = new StringTokenizer(s); int ntok = st.countTokens(); if(ntok == 1){ assert st.nextToken().equals("print"); t.print(0); out.print(hyphen60); continue; } if(ntok == 2){ String s1 = st.nextToken(), s2 = st.nextToken(); assert s1.equals("fire"); Tree.fire(s2); continue; } String s1 = st.nextToken(), s2 = st.nextToken(), s3 = st.nextToken(); assert s2.equals("hires"); Tree.hire(s1, s3); } out.flush(); } } class Tree{ static Map<String, Tree> map = new HashMap<String, Tree>(); String name; Tree parent; LinkedList<Tree> children; Tree(String n, Tree p){ name = n; parent = p; map.put(n, this); children = new LinkedList<Tree>(); } static void fire(String name){ map.get(name).fired(); } void fired(){ if(children.isEmpty()){ parent.children.remove(this); return; } Tree child = children.getFirst(); name = child.name; child.fired(); map.put(name, this); } static void hire(String hirer, String hiree){ Tree t = map.get(hirer); t.children.add(new Tree(hiree, t)); } void print(int depth){ for(int i=0; i<depth; i++){ Main.out.print('+'); } Main.out.println(name); for(Tree t : children){ t.print(depth+1); } } }