本文主要是介绍zoj 3480,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一次用java交代吗: 各种错误a
import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set;public class Main{public static void main(String args[]) {Scanner cin=new Scanner(System.in);int t=cin.nextInt();while((t--)>0){classload load=new classload();while(cin.hasNextLine()){String line=cin.nextLine();int indexof=line.indexOf(' ');if(indexof==-1){load.slove(line);}else {load.slove(line.substring(0, indexof),line.substring(indexof+1));}if(load.end){break;}}System.out.println();}}private static class classload{private boolean begin=false;private boolean end=false;private final Map<String,String>classMap=new HashMap<String,String>(500);private final Map<String,Set<String>>methodMap=new HashMap<String,Set<String>>(500);public void slove(String name){if(name.length()==0){return;}if(name.equals("begin")){begin=true;}else if(name.equals("end")){end=true;}}public void slove(String name,String cmp){if(name.equals("class")) defineClass(cmp);else if(name.equals("call")) call(cmp);else if(name.equals("def")) defineMethod(cmp);else if(name.equals("undef")) undefineMethod(cmp);}private void undefineMethod(String cmp) {int indexof=cmp.indexOf('.');if(indexof==-1){oops();return;}undefineMethod0(cmp.substring(0,indexof),cmp.substring(indexof+1));}private void undefineMethod0(String substring, String substring2) {Set<String>set=methodMap.get(substring);if(set==null||!set.contains(substring2)){oops();return;}set.remove(substring2);System.out.println("undef "+substring+"."+substring2);}private void defineMethod(String cmp) {int indexof=cmp.indexOf('.');if(indexof==-1){oops();return;}defineMethod0(cmp.substring(0, indexof),cmp.substring(indexof+1));}private void defineMethod0(String classname, String substring2) {if(!classMap.containsKey(classname)){oops();return;}Set<String> method=methodMap.get(classname);if(method==null){method=new HashSet<String>(500);methodMap.put(classname, method);}if(method.contains(substring2)){System.out.println("redef "+classname+"."+substring2);return;}method.add(substring2);System.out.println("def "+classname+"."+substring2);}private void call(String cmp) {int indexof=cmp.indexOf('.');if(indexof==-1){oops();return;}call0(cmp.substring(0, indexof),cmp.substring(indexof+1));}private void call0(String classname, String methodname) {if(!classMap.containsKey(classname)){oops();return ;}//System.out.println("aaaaaaaa");while(classname!=null){Set<String>methods=methodMap.get(classname);if(methods==null||!methods.contains(methodname)){classname=classMap.get(classname);continue;}System.out.println("invoke "+classname+"."+methodname);return;}oops();}private void defineClass(String cmp) {int indexof=cmp.indexOf(':');if(indexof==-1){if(classMap.containsKey(cmp)){oops();return;}classMap.put(cmp, null);System.out.println("class "+cmp);return;}if(!classMap.containsKey(cmp.substring(indexof+1))||classMap.containsKey(cmp.substring(0, indexof))){oops();return;}classMap.put(cmp.substring(0,indexof), cmp.substring(indexof+1));System.out.println("class "+cmp);return;}}private static void oops(){System.out.println("oops!");} } /* 3 begin class Sub:Super class Super class Sub:Super endbegin class Class call Class.Method def Class.Method call Class.Method endbegin class Super class Sub:Super def Super.Method call Sub.Method class Sub:Super call sub:aaaa end*/
这篇关于zoj 3480的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!