HDU1880

简介: 题意就是根据咒语查功能,根据功能查看是否存在相应咒语,题意简单,不过是道不错的练习题。         下面的都MLE了,听说C++用G++提交才可以AC,否则也MLE;方法很多,不想做了……         方法一:我用Java的HashMap一直MLE,即便由value反查key减少映射数也一样MLE,听说C++的map可以AC。

        题意就是根据咒语查功能,根据功能查看是否存在相应咒语,题意简单,不过是道不错的练习题。

        下面的都MLE了,听说C++用G++提交才可以AC,否则也MLE;方法很多,不想做了……

        方法一:我用Java的HashMap一直MLE,即便由value反查key减少映射数也一样MLE,听说C++的map可以AC。

        方法二:快排+二分

        方法三:Hash方法,用31求幂。

import java.util.*;

//写俩Map
public class HDU1880 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =null;
        Map<String,String> map1 = new HashMap<String,String>();
        Map<String,String> map2 = new HashMap<String,String>();
        String[] s0;
        while(true) {
            str = sc.nextLine();
            if(str.equals("@END@")) {
                break;
            }
            s0 = str.split(" ", 2);
            str = s0[0].substring(1,s0[0].length()-1);
            map1.put(str,s0[1]);
            map2.put(s0[1], str);
        }
        int n = sc.nextInt();
        for(int i=0; i<n; i++) {
            String s = sc.next();
            s += sc.nextLine();
            if(s.charAt(0)=='[') {
                s = s.substring(1, s.length()-1);
                boolean tag = map1.containsKey(s);
                if(tag) {
                    System.out.println(map1.get(s));
                }else {
                    System.out.println("what?");
                }
            }else {
                boolean tag = map2.containsKey(s);
                if(tag) {
                    System.out.println(map2.get(s));
                }else {
                    System.out.println("what?");
                }
            }
        }
    }
}
import java.util.*;
//一个Map存两次
public class HDU1880 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =null;
        Map<String,String> map = new HashMap<String,String>();
        String[] s0;
        while(true) {
            str = sc.nextLine();
            if(str.equals("@END@")) {
                break;
            }
            s0 = str.split(" ", 2);
            str = s0[0].substring(1,s0[0].length()-1);
            map.put(str,s0[1]);
            map.put(s0[1], str);
        }
        int n = sc.nextInt();
        for(int i=0; i<n; i++) {
            String s = sc.next();
            s += sc.nextLine();
            if(s.charAt(0)=='[') {
                s = s.substring(1, s.length()-1);
            }
            boolean tag = map.containsKey(s);
            if(tag) {
                System.out.println(map.get(s));
            }else {
                System.out.println("what?");
            }
        }
    }
}
import java.util.*;
//map里反查key
public class HDU1880 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =null;
        Map<String,String> map = new HashMap<String,String>();
        String[] s0;
        while(true) {
            str = sc.nextLine();
            if(str.equals("@END@")) {
                break;
            }
            s0 = str.split(" ", 2);
            str = s0[0].substring(1,s0[0].length()-1);
            map.put(str,s0[1]);
        }
        int n = sc.nextInt();
        for(int i=0; i<n; i++) {
            String s = sc.next();
            s += sc.nextLine();
            if(s.charAt(0)=='[') {
                s = s.substring(1, s.length()-1);
                boolean tag = map.containsKey(s);
                if(tag) {
                    System.out.println(map.get(s));
                }else {
                    System.out.println("what?");
                }
            }else {
                ArrayList<String> arr = valueGetKey(map,s);
                if(arr.isEmpty()) {
                    System.out.println("what?");
                }else {
                    System.out.println(arr.get(0));
                }
            }
            
        }
    }

    private static ArrayList<String> valueGetKey(Map<String, String> map,String value) {

        Set set = map.entrySet();
        ArrayList<String> arr = new ArrayList<String>();
        Iterator<Map.Entry<String, String> > it = set.iterator();
        while(it.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>)it.next();
            if(entry.getValue().equals(value)) {
                String s = entry.getKey();
                arr.add(s);
            }
        }
        return arr;
    }
}
目录
相关文章
|
Java BI
HDU 1412 {A} + {B}
{A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19833    Accepted Submission(s): 8245 Problem Description 给你两个集合,要求{A} + {B}.
837 0