Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

超级慢又吃内存的java解. 用了16M内存,汗颜.

Posted by aa3x at 2019-08-06 16:46:52 on Problem 1002
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LinkedHashMap<String, Integer> hash = new LinkedHashMap<String, Integer>();
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                String next = scanner.next();
                String code = normalize(next);
                Integer value = hash.get(code);
                hash.put(code, value == null ? 1 : value + 1);
            }
        }

        LinkedList<String> list = new LinkedList<String>();
        for (Map.Entry<String, Integer> entry : hash.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            if (value > 1) {
                list.add(key + " " + value);
            }
        }
        if (list.isEmpty()) {
            System.out.println("No duplicates.");
            return;
        }
        Collections.sort(list);

        for (String s : list) {
            System.out.println(s);
        }
    }

    private static String normalize(String next) {
        StringBuilder sb = new StringBuilder();
        for (char c : next.toCharArray()) {
            //@formatter:off
            if (c == '-') { continue; }
            //@formatter:on
            String n = map.get(c);
            if (n == null) {
                continue;
            }
            if (sb.length() == 3) {
                sb.append("-");
            }
            sb.append(n);
        }
        return sb.toString();
    }

    static HashMap<Character, String> map;

    static {
        map = new HashMap<Character, String>();
        String[] s = ("A, B, 和C 映射到 2 \n" +
                "D, E, 和F 映射到 3 \n" +
                "G, H, 和I 映射到 4 \n" +
                "J, K, 和L 映射到 5 \n" +
                "M, N, 和O 映射到 6 \n" +
                "P, R, 和S 映射到 7 \n" +
                "T, U, 和V 映射到 8 \n" +
                "W, X, 和Y 映射到 9 ").split("\\n");
        for (String s1 : s) {
            String[] s2 = s1.split("映射到");
            String n = s2[1].trim();
            String[] s3 = s2[0].replaceFirst("和", "").split(",");
            for (String s4 : s3) {
                String k = s4.trim();
                map.put(k.toCharArray()[0], n);
            }
        }
        for (int i = 0; i < 10; i++) {
            map.put((i + "").toCharArray()[0], i + "");
        }
    }

    ;
}








测试写法


import org.junit.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringBufferInputStream;

/**
 * @author
 */
public class MainTest {
    static {
    }

    @DataProvider
    public Object[][] dp() {
        return new Object[][]{
                {"12\n" +
                        "4873279\n" +
                        "ITS-EASY\n" +
                        "888-4567\n" +
                        "3-10-10-10\n" +
                        "888-GLOP\n" +
                        "TUT-GLOP\n" +
                        "967-11-11\n" +
                        "310-GINO\n" +
                        "F101010\n" +
                        "888-1200\n" +
                        "-4-8-7-3-2-7-9-\n" +
                        "487-3279", "310-1010 2\n" +
                        "487-3279 4\n" +
                        "888-4567 3"}
        };
    }

    @Test(dataProvider = "dp")
    public void test(String x, String r) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream oldout = System.out;
        System.setOut(new PrintStream(out));
        System.setIn(new StringBufferInputStream(x));

        Main.main(null);
        String s = new String(out.toByteArray());

        Assert.assertEquals( r,s);
    }

}










Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator