Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Main {
- static TreeMap<String, String> treeMap = new TreeMap<String, String>();
- static HashMap<String, String> number_value = new HashMap<>();
- static HashMap<Integer, String> dictionary = new HashMap<>();
- public static void main(String[] args) throws IOException {
- dictionary.put(0, "oqz");
- dictionary.put(1, "ij");
- dictionary.put(2, "abc");
- dictionary.put(3, "def");
- dictionary.put(4, "gh");
- dictionary.put(5, "kl");
- dictionary.put(6, "mn");
- dictionary.put(7, "rps");
- dictionary.put(8, "tuv");
- dictionary.put(9, "wxy");
- start_program();
- }
- public static void start_program() throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- String enter_value = "";
- String phone_number = "";
- int array_map = 0;
- while (true) {
- enter_value = bufferedReader.readLine();
- if (enter_value.equals("-1")) {
- break;
- }
- phone_number = enter_value;
- array_map = Integer.parseInt(bufferedReader.readLine());
- String[] enter_words = new String[array_map];
- for (int i = 0; i < enter_words.length; i++) {
- enter_value = bufferedReader.readLine();
- enter_words[i] = enter_value;
- }
- digitization_words(enter_words, number_value, dictionary);
- sort();
- work_prog(treeMap, phone_number);
- }
- }
- public static void digitization_words(String[] enter_words, HashMap<String, String> number_value, HashMap<Integer, String> dictionary) {
- String number = "";
- for (int i = 0; i < enter_words.length; i++) { // перевод слова в цифровое значение
- try {
- char[] wood = enter_words[i].toCharArray();
- for (int j = 0; j < wood.length; j++) {
- for (Map.Entry<Integer, String> map : dictionary.entrySet()) {
- int index = map.getValue().indexOf(wood[j]);
- if (index != -1) {
- number += String.valueOf(map.getKey());
- break;
- }
- }
- }
- number_value.put(number, enter_words[i]);
- number = "";
- } catch (NullPointerException e) {
- e.getMessage();
- }
- }
- }
- public static void sort() {
- treeMap = new TreeMap<String, String>( // сортирует словарь от большего к меньшему
- new Comparator<String>() {
- @Override
- public int compare(String s1, String s2) {
- if (s1.length() > s2.length()) {
- return -1;
- } else if (s1.length() < s2.length()) {
- return 1;
- } else {
- return s1.compareTo(s2);
- }
- }
- });
- treeMap.putAll(number_value);
- }
- public static void work_prog(TreeMap<String, String> treeMap, String phone_number) {
- String res = "";
- String result = "";
- for (int i = 0; i < treeMap.size(); i++) {
- for (Map.Entry<String, String> map : treeMap.entrySet()) {
- if (phone_number.length() == 0) {
- break;
- }
- if (phone_number.length() >= map.getKey().length()) {
- res = phone_number.substring(0, map.getKey().length());
- if (map.getKey().equals(res)) {
- result += map.getValue() + " ";
- phone_number = phone_number.substring(map.getKey().length(), phone_number.length());
- }
- }
- }
- if (i == treeMap.size() - 1 && phone_number.length() > 0) {
- System.out.println("No solution");
- } else if (i == treeMap.size() - 1 && phone_number.length() == 0) {
- System.out.println(result);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement