Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.     static TreeMap<String, String> treeMap = new TreeMap<String, String>();
  11.     static HashMap<String, String> number_value = new HashMap<>();
  12.     static HashMap<Integer, String> dictionary = new HashMap<>();
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.  
  16.         dictionary.put(0, "oqz");
  17.         dictionary.put(1, "ij");
  18.         dictionary.put(2, "abc");
  19.         dictionary.put(3, "def");
  20.         dictionary.put(4, "gh");
  21.         dictionary.put(5, "kl");
  22.         dictionary.put(6, "mn");
  23.         dictionary.put(7, "rps");
  24.         dictionary.put(8, "tuv");
  25.         dictionary.put(9, "wxy");
  26.  
  27.         start_program();
  28.     }
  29.  
  30.     public static void start_program() throws IOException {
  31.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  32.         String enter_value = "";
  33.         String phone_number = "";
  34.         int array_map = 0;
  35.  
  36.         while (true) {
  37.             enter_value = bufferedReader.readLine();
  38.             if (enter_value.equals("-1")) {
  39.                 break;
  40.             }
  41.  
  42.             phone_number = enter_value;
  43.             array_map = Integer.parseInt(bufferedReader.readLine());
  44.             String[] enter_words = new String[array_map];
  45.  
  46.             for (int i = 0; i < enter_words.length; i++) {
  47.                 enter_value = bufferedReader.readLine();
  48.                 enter_words[i] = enter_value;
  49.             }
  50.  
  51.             digitization_words(enter_words, number_value, dictionary);
  52.             sort();
  53.             work_prog(treeMap, phone_number);
  54.         }
  55.     }
  56.  
  57.     public static void digitization_words(String[] enter_words, HashMap<String, String> number_value, HashMap<Integer, String> dictionary) {
  58.         String number = "";
  59.         for (int i = 0; i < enter_words.length; i++) { // перевод слова в цифровое значение
  60.             try {
  61.                 char[] wood = enter_words[i].toCharArray();
  62.                 for (int j = 0; j < wood.length; j++) {
  63.                     for (Map.Entry<Integer, String> map : dictionary.entrySet()) {
  64.                         int index = map.getValue().indexOf(wood[j]);
  65.                         if (index != -1) {
  66.                             number += String.valueOf(map.getKey());
  67.                             break;
  68.                         }
  69.                     }
  70.                 }
  71.                 number_value.put(number, enter_words[i]);
  72.                 number = "";
  73.             } catch (NullPointerException e) {
  74.                 e.getMessage();
  75.             }
  76.         }
  77.     }
  78.  
  79.     public static void sort() {
  80.         treeMap = new TreeMap<String, String>( // сортирует словарь от большего к меньшему
  81.                 new Comparator<String>() {
  82.                     @Override
  83.                     public int compare(String s1, String s2) {
  84.                         if (s1.length() > s2.length()) {
  85.                             return -1;
  86.                         } else if (s1.length() < s2.length()) {
  87.                             return 1;
  88.                         } else {
  89.                             return s1.compareTo(s2);
  90.                         }
  91.                     }
  92.                 });
  93.         treeMap.putAll(number_value);
  94.     }
  95.  
  96.     public static void work_prog(TreeMap<String, String> treeMap, String phone_number) {
  97.         String res = "";
  98.         String result = "";
  99.  
  100.         for (int i = 0; i < treeMap.size(); i++) {
  101.             for (Map.Entry<String, String> map : treeMap.entrySet()) {
  102.                 if (phone_number.length() == 0) {
  103.                     break;
  104.                 }
  105.                 if (phone_number.length() >= map.getKey().length()) {
  106.                     res = phone_number.substring(0, map.getKey().length());
  107.                     if (map.getKey().equals(res)) {
  108.                         result += map.getValue() + " ";
  109.                         phone_number = phone_number.substring(map.getKey().length(), phone_number.length());
  110.                     }
  111.                 }
  112.             }
  113.             if (i == treeMap.size() - 1 && phone_number.length() > 0) {
  114.                 System.out.println("No solution");
  115.             } else if (i == treeMap.size() - 1 && phone_number.length() == 0) {
  116.                 System.out.println(result);
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement