ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1002. Phone Numbers

maxormo WA 3 java [4] // Problem 1002. Phone Numbers 7 Jul 2015 03:50
my solution passing all test from my head
can you advice what's wrong:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.util.stream.Collectors.toList;

/**
 * solution for http://acm.timus.ru/problem.aspx?space=1&num=1002
 */
public class Step {

    private static Map<Character, List<Character>> mapping;

    public static void main(String[] args) throws IOException {
        new Step().solveMe(new BufferedReader(new InputStreamReader(System.in)));
    }

    public static Map<Character, List<Character>> buildMapping() {
        Map<Character, List<Character>> map = new HashMap<>();
        map.put('1', Arrays.asList('i', 'j'));
        map.put('2', Arrays.asList('a', 'b', 'c'));
        map.put('3', Arrays.asList('d', 'e', 'f'));
        map.put('4', Arrays.asList('g', 'h'));
        map.put('5', Arrays.asList('k', 'l'));
        map.put('6', Arrays.asList('m', 'n'));
        map.put('7', Arrays.asList('p', 'r', 's'));
        map.put('8', Arrays.asList('t', 'u', 'v'));
        map.put('9', Arrays.asList('w', 'x', 'y'));
        map.put('0', Arrays.asList('o', 'q', 'z'));
        return map;
    }

    public static Trie buildTrie(List<String> dictionary) {
        return dictionary.stream().reduce(
                Trie.EMPTY_TRIE, (node, word) -> node.buildTree(node, word),
                (node, node2) -> node.append(node));
    }

    private void solveMe(BufferedReader in) throws IOException {
        String n;
        mapping = buildMapping();
        Map<String, Trie> input = new HashMap<>();
        while (!(n = in.readLine()).equals("-1")) {
            int s = Integer.parseInt(in.readLine());
            List<String> dict = in.lines().limit(s).collect(toList());
            input.compute(n, (key, val) -> buildTrie(dict));
        }
        input.forEach((s, trie) -> System.out.println(solution(s, trie)));
    }

    public String solution(String number, Trie root) {

        List<List<String>> result = root.filter(number);
        if (result.isEmpty()) {
            return "No solution.";
        }
        List<String> bestShot = result.stream().reduce((List<String> minList, List<String> other) -> {
            if (minList.size() >= other.size()) {
                return other;
            }
            return minList;
        }).get();
        return String.join(" ", bestShot);
    }

    public static class Trie {

        public static final Trie EMPTY_TRIE = new Trie((char) 0);
        private final Character ch;
        private final Map<Character, Trie> nexts;
        private final boolean isWord;

        public Trie(Character ch) {
            this(ch, new HashMap<>(), false);
        }

        private Trie(Character ch, boolean isWord) {
            this(ch, new HashMap<>(), isWord);
        }

        private Trie(Character ch, Map<Character, Trie> nexts, boolean isWord) {
            this.ch = ch;
            this.nexts = nexts;
            this.isWord = isWord;
        }

        public List<List<String>> filter(String number) {
            List<List<String>> lists = new ArrayList<>();
            List<String> e = new ArrayList<>();
            filterHelper(lists, e, number, "", this);
            return lists;
        }

        private void filterHelper(List<List<String>> acc, List<String> result, String number, String word, Trie node) {
            if (node.isWord) {
                if (number.length() == 0) {
                    result.add(word);
                    acc.add(result);
                    return;
                }
                List<String> res = new ArrayList<>(result);
                res.add(word);
                filterHelper(acc, res, number, "", this);
            }

            List<Character> maps = mapping.get(number.charAt(0));
            maps.forEach(e -> {
                Trie next = node.nexts.get(e);
                if (next != null) {
                    filterHelper(acc, new ArrayList<>(result), number.substring(1), word + next.ch, next);
                }
            });
        }

        public Trie buildTree(Trie root, String word) {
            char c = word.charAt(0);
            if (word.length() == 1) {
                Trie newTrie = new Trie(c, true);
                return root.append(newTrie);
            } else {
                Trie newTrie = new Trie(c, false);
                return root.append(buildTree(newTrie, word.substring(1)));
            }
        }

        public Trie append(Trie other) {
            Trie trieCopy = new Trie(this.ch, this.nexts, this.isWord);
            Trie newVal = other;
            if (trieCopy.nexts.containsKey(newVal.ch)) {
                Trie next = trieCopy.nexts.get(newVal.ch);
                Trie initial = new Trie(next.ch, next.nexts, next.isWord);
                newVal = newVal.nexts.values().stream().reduce(initial, Trie::append);
            }
            trieCopy.nexts.put(newVal.ch, newVal);
            return trieCopy;
        }

        @Override
        public String toString() {
            StringBuilder bld = new StringBuilder();
            bld.append(ch).append(" = [");
            nexts.forEach((c, n) -> bld.append(n).append(" "));
            return bld.append("]").append(isWord ? " = true" : "").toString();
        }
    }
}
maxormo Re: WA 3 java [3] // Problem 1002. Phone Numbers 8 Jul 2015 01:32
test that my code didn't pass
12345
5
iad
i
adgk
g
k
-1
raven Re: WA 3 java // Problem 1002. Phone Numbers 13 Jan 2016 01:22
Thank you!
kami_botanik Re: WA 3 java // Problem 1002. Phone Numbers 11 Mar 2016 09:42
I passed your test, but I still can't get pass from test 3. Maybe there have some different condition?
Sable Re: WA 3 java // Problem 1002. Phone Numbers 9 Jun 2020 00:09
maxormo, thank you very much! Your test helped me to find mistake in my algoritm.

Edited by author 09.06.2020 00:10