00vk

task2209/Solution

Sep 30th, 2019 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package com.javarush.task.task22.task2209;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.stream.*;
  6.  
  7. /*
  8. Составить цепочку слов
  9.  
  10. Для теста:
  11. Abstract ArrayList Callback Database Delegation Entity Enum EnumMap EnumSet Enumeration Event Exception ExceptionHandling Expression Generics Java KeyStore Kotlin Lambda LinkedList Lombok Loop Member MessageDigest NIO Namespace Nashorn Native Nested Null NullPointer NullType Numeric Object Package Polymorphism Recursion Regex RuntimeException Socket Thread ThreadPool Throwable Timer Token Transaction Type TypeInference XML YAML
  12. */
  13.  
  14. public class Solution {
  15.  
  16.     public static void main(String[] args) {
  17.         BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));
  18.         try (BufferedReader fileReader = new BufferedReader(new FileReader(inReader.readLine()))) {
  19.             String content = fileReader.lines().collect(Collectors.joining(" "));
  20.             String[] words = content.trim().split("\\s+");
  21.             StringBuilder result = getLine(words);
  22.             System.out.println(result);
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.  
  29.     public static StringBuilder getLine(String... words) {
  30.         if (words.length == 0) {
  31.             return new StringBuilder();
  32.         }
  33.  
  34.         DirectedMultigraph graph = new DirectedMultigraph(words);
  35.         return new StringBuilder(graph.findEulerianTrail());
  36.     }
  37. }
  38.  
  39. class DirectedMultigraph {
  40.  
  41.     private final Map<Character, Set<Arc>> vertices = new HashMap<>();
  42.     private final Map<Character, Integer> arcBalances = new HashMap<>();
  43.     private final List<Arc> trail = new ArrayList<>();
  44.  
  45.     public DirectedMultigraph(String... words) {
  46.         Arrays.stream(words).forEach(this::addArc);
  47.     }
  48.  
  49.     public String findEulerianTrail() {
  50.         depthFirstSearch(getTrailStart());
  51.         return trail.stream().map(DirectedMultigraph.Arc::toString)
  52.                 .collect(Collectors.joining(" "));
  53.     }
  54.  
  55.     private void addArc(String word) {
  56.         Arc arc = new Arc(word);
  57.         char begin = arc.getFirstChar();
  58.         char end = arc.getLastChar();
  59.         Stream.of(begin, end).forEach(vertex -> vertices.putIfAbsent(vertex, new HashSet<>()));
  60.         vertices.get(begin).add(arc);
  61.         arcBalances.put(begin, arcBalances.getOrDefault(begin, 0) + 1);
  62.         arcBalances.put(end, arcBalances.getOrDefault(end, 0) - 1);
  63.     }
  64.  
  65.     private char getTrailStart() {
  66.         char start = 0;
  67.         char finish = 0;
  68.         for (char vertex : vertices.keySet()) {
  69.             int diff = arcBalances.get(vertex);
  70.             if (diff == 1) {
  71.                 start = validateVertex(start, vertex);
  72.             } else if (diff == -1) {
  73.                 finish = validateVertex(finish, vertex);
  74.             } else if (diff != 0) {
  75.                 throw new RuntimeException("Not an Eulerian graph!");
  76.             }
  77.         }
  78.         if (start == 0) {
  79.             start = vertices.keySet().iterator().next();
  80.         }
  81.         return start;
  82.     }
  83.  
  84.     private char validateVertex(char current, char vertex) {
  85.         if (current != 0) {
  86.             throw new RuntimeException("Not an Eulerian graph!");
  87.         }
  88.         return vertex;
  89.     }
  90.  
  91.  
  92.     private void depthFirstSearch(char sourceVertex) {
  93.         for (Arc arc : vertices.get(sourceVertex)) {
  94.             if (!arc.isUsed()) {
  95.                 arc.setUsed();
  96.                 depthFirstSearch(arc.getLastChar());
  97.                 trail.add(0, arc);
  98.             }
  99.         }
  100.     }
  101.  
  102.     @Override
  103.     public String toString() {
  104.         return vertices.toString();
  105.     }
  106.  
  107.     static class Arc {
  108.  
  109.         private final String word;
  110.         private boolean isUsed = false;
  111.  
  112.         private Arc(String word) {
  113.             this.word = word;
  114.         }
  115.  
  116.         private void setUsed() {
  117.             isUsed = true;
  118.         }
  119.  
  120.         @Override
  121.         public String toString() {
  122.             return word;
  123.         }
  124.  
  125.         public boolean isUsed() {
  126.             return isUsed;
  127.         }
  128.  
  129.         public char getFirstChar() {
  130.             return Character.toLowerCase(word.charAt(0));
  131.         }
  132.  
  133.         public char getLastChar() {
  134.             return Character.toLowerCase(word.charAt(word.length() - 1));
  135.         }
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment