ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1002. Телефонные номера

test 5 time limit
Послано Bohdan 12 сен 2015 03:22
222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322
5
a
aa
afa
aaaa
aafaa
-1

this test solved very quickly.


my code:

public class T1002 {

    public static int nnn = 100000;
    public static String sss = "";

    public static int nnn2 = 100000;
    public static String sss2 = "";

    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(System.out);

        HashMap<String,String> hashMap = new HashMap<>();

        while (true) {
            String s = sc.nextLine();

            int length = s.length();

            if (s.charAt(0) == '-' && s.charAt(1) == '1') {
                break;
            } else{
                int n = sc.nextInt();
                for(int i = 0;i<n;i++){
                    String s1 = sc.nextLine();
                    String s2 = change(s1);
                    hashMap.put(s2,s1);
                }
                solve(0,length,s,hashMap,new StringBuilder(""));
                solve2(0,length,s,hashMap,new StringBuilder(""));
                if(nnn == 100000 && nnn2 == 100000){
                    out.println("No solution.");
                } else {
                    if(nnn<nnn2){
                        out.println(sss);
                    } else {
                        out.println(sss2);
                    }
                    nnn = 100000;
                    sss = "";
                    nnn2 = 100000;
                    sss2 = "";
                }
                hashMap.clear();
            }
        }

        out.close();
    }

    public static void solve(int x,int y,String s,HashMap<String,String> hashMap,StringBuilder sb){
        if(x == y){
            nnn = sb.length();
            sss = sb.toString();
        } else if(x != 0){
            sb.append(" ");
        }

        for(int i = y;i>x;i--){
            if(hashMap.containsKey(s.substring(x,i))){
                String s1 = hashMap.get(s.substring(x,i));
                if(nnn == 100000) {
                    solve(x + s1.length(), y, s, hashMap,sb.append(s1));
                }
            }
        }


    }

    public static void solve2(int x,int y,String s,HashMap<String,String> hashMap,StringBuilder sb) {
        if (x == y) {
            nnn2 = sb.length();
            sss2 = sb.toString();
        } else if (y != s.length()) {
            sb.insert(0," ");
        }

        for(int i = x;i<y;i++){
            if(hashMap.containsKey(s.substring(i,y))){
                String s1 = hashMap.get(s.substring(i,y));
                if(nnn2 == 100000) {
                    solve2(x, y - s1.length(), s, hashMap,sb.insert(0,s1));
                }
            }
        }
    }

    public static String change(String s){
        StringBuilder sb = new StringBuilder("");
        for(int i = 0;i<s.length();i++){
            if(s.charAt(i) == 'i' || s.charAt(i) == 'j'){
                sb.append("1");
            } else if(s.charAt(i) == 'g' || s.charAt(i) == 'h'){
                sb.append("4");
            } else if(s.charAt(i) == 'k' || s.charAt(i) == 'l'){
                sb.append("5");
            } else if(s.charAt(i) == 'm' || s.charAt(i) == 'n'){
                sb.append("6");
            } else if(s.charAt(i) == 'p' || s.charAt(i) == 'r' || s.charAt(i) == 's'){
                sb.append("7");
            } else if(s.charAt(i) == 'a' || s.charAt(i) == 'b' || s.charAt(i) == 'c'){
                sb.append("2");
            } else if(s.charAt(i) == 't' || s.charAt(i) == 'u' || s.charAt(i) == 'v'){
                sb.append("8");
            } else if(s.charAt(i) == 'o' || s.charAt(i) == 'q' || s.charAt(i) == 'z'){
                sb.append("0");
            } else if(s.charAt(i) == 'd' || s.charAt(i) == 'e' || s.charAt(i) == 'f'){
                sb.append("3");
            } else if(s.charAt(i) == 'w' || s.charAt(i) == 'x' || s.charAt(i) == 'y'){
                sb.append("9");
            }
        }
        return sb.toString();
    }

    public static class MyScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }

}