Advertisement
Alisator

PAL6_nedodelanyPruchod

Dec 17th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Map.Entry;
  10. import java.util.Set;
  11. import java.util.StringTokenizer;
  12.  
  13. public class Main {
  14.  
  15.     /*The task
  16.      We say that a sequence of transformations producing W2 from W1 is optimal if there
  17.      is not any shorter sequence producing W2 from W1. You are given a vocabulary and
  18.      a bound on the number of single edit operations allowed per one transformation.
  19.      To quantify how difficult assignments can be created, your task is to compute
  20.      the maximum of lengths of all optimal sequences of transformations where the
  21.      starting and target word can be chosen arbitrarily.
  22.  
  23.      Input
  24.      The first line of the input contains two integers L, N separated by space. L is
  25.      the maximal number of single edit operations allowed per one transformation.
  26.      N is the number of words in the vocabulary. Next, there are N lines, each of
  27.      them containing one word of the vocabulary. A word is a string having characters
  28.      in the range 'a' - 'z'. The length of every word is at most 20 characters.
  29.      The value of L does not exceed 5, the value of N does not exceed 5000.
  30.  
  31.      Output
  32.      The output contains one text line with single integer representing the maximal
  33.      length of optimal sequences of transformations.
  34.      */
  35.     public static int countOfEditOneTrans; //L - neni vyresena kontrola
  36.     public static int countOfWords; //N
  37.     public static String[] vocabulary;
  38.     public static int[][] dist;
  39.     public static HashSet[] cesty;
  40.  
  41.     public static HashMap<String, Set<Word>> LevGraph;
  42.     public static Set<Integer> dijkstraQ;
  43.  
  44.     public static void main(String[] args) throws IOException {
  45.         args = new String[]{"pub03.in"};
  46.         BufferedReader br = (args.length == 0)
  47.                 ? new BufferedReader(new InputStreamReader(System.in))
  48.                 : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
  49.  
  50.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  51.         countOfEditOneTrans = Integer.parseInt(configuration.nextToken());
  52.         countOfWords = Integer.parseInt(configuration.nextToken());
  53.         vocabulary = new String[countOfWords];
  54.         LevGraph = new HashMap<>();
  55.         cesty = new HashSet[countOfWords];
  56.         for (int i = 0; i < countOfWords; i++) {
  57.             add(br.readLine());
  58.         }
  59.         printGraph();
  60.     }
  61.  
  62.     private static int computeDTW(String base, String compare) {
  63.         int bLength = base.length() + 1;
  64.         int cLength = compare.length() + 1;
  65.         int[] DTWdist = new int[bLength]; //ceny transofmraci
  66.         int[] DTWdisthelp = new int[bLength];
  67.         int match; //shoda
  68.         int insertion; //vlozeni
  69.         int deletion; //smazani
  70.         int replacement; //nahrazeni
  71.         int[] temp;
  72.  
  73.         for (int b = 0; b < bLength; b++) {
  74.             DTWdist[b] = b;
  75.         }
  76.         for (int c = 1; c < cLength; c++) {
  77.             DTWdisthelp[0] = c; //na zacatek nula, proto od indexu 1 a delky jsou +1
  78.             for (int b = 1; b < bLength; b++) {
  79.                 //pismenka se se shoduji
  80.                 if (base.charAt(b - 1) == compare.charAt(c - 1)) {
  81.                     match = 0;
  82.                 } else {
  83.                     match = 1;
  84.                 }
  85.                 //vypocty pro jednotlive operace - pricteme jim +1 a vybereme tu
  86.                 insertion = DTWdist[b] + 1;
  87.                 replacement = DTWdist[b - 1] + match;
  88.                 deletion = DTWdisthelp[b - 1] + 1;
  89.                 DTWdisthelp[b] = minimumCost(insertion, deletion, replacement);
  90.             }
  91.             temp = DTWdist;
  92.             DTWdist = DTWdisthelp;
  93.             DTWdisthelp = temp;
  94.         }
  95.         return DTWdist[bLength - 1];
  96.     }
  97.  
  98.     private static int minimumCost(int a, int b, int c) {
  99.         return Math.min(Math.min(a, b), c);
  100.     }
  101.  
  102.     private static void add(String word) {
  103.         int levDist;
  104.         if (LevGraph.containsKey(word) == false) {
  105.             LevGraph.put(word, new HashSet<>());
  106.         }
  107.  
  108.         for (String s : LevGraph.keySet()) {
  109.             //splnena podminka poctu operaci, udelame propojeni
  110.             levDist = computeDTW(word, s);
  111.             if (word.equals(s) == false && levDist <= countOfEditOneTrans) {
  112.                 if (!LevGraph.containsKey(s)) {
  113.                     LevGraph.put(s, new HashSet<>());
  114.                 }
  115.                 LevGraph.get(word).add(new Word(s, levDist));
  116.                 LevGraph.get(s).add(new Word(word, levDist));
  117.             }
  118.         }
  119.     }
  120.  
  121.     private static class Word {
  122.  
  123.         int levDist;
  124.         String word;
  125.  
  126.         public Word(String word, int levDist) {
  127.             this.levDist = levDist;
  128.             this.word = word;
  129.         }
  130.  
  131.         @Override
  132.         public String toString() {
  133.             return "word " + this.word + " L: " + this.levDist + " ";
  134.  
  135.         }
  136.  
  137.     }
  138.  
  139.     ///eeee pseudo pokus od pruchod
  140.     private static void findLongestWay(String s, int i) {
  141.         int max = 0;
  142.         String last = "";
  143.         cesty[i] = new HashSet();
  144.         int count = 0;
  145.         for (Entry<String, Set<Word>> entry : LevGraph.entrySet()) { //pro kazdy jeho entry
  146.             if (cesty[i].add(entry.getValue())) {
  147.                 cesty[i].add(entry.getValue());
  148.                 last = entry.getValue().toString();
  149.                 System.out.println(last);
  150.                 count++;
  151.             }
  152.         }
  153.         if (count > max) {
  154.             max = count;
  155.         }
  156.         findLongestWay(last, i);
  157.  
  158.     }
  159.  
  160.     public static void printGraph() {
  161.         for (Entry<String, Set<Word>> entry : LevGraph.entrySet()) {
  162.             System.out.println(entry.getKey() + ": " + entry.getValue().toString());
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement