Advertisement
Alisator

PAL6_nevyreseneL

Dec 14th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 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.StringTokenizer;
  8.  
  9. public class Main {
  10.  
  11.     /*The task
  12.      We say that a sequence of transformations producing W2 from W1 is optimal if there
  13.      is not any shorter sequence producing W2 from W1. You are given a vocabulary and
  14.      a bound on the number of single edit operations allowed per one transformation.
  15.      To quantify how difficult assignments can be created, your task is to compute
  16.      the maximum of lengths of all optimal sequences of transformations where the
  17.      starting and target word can be chosen arbitrarily.
  18.  
  19.      Input
  20.      The first line of the input contains two integers L, N separated by space. L is
  21.      the maximal number of single edit operations allowed per one transformation.
  22.      N is the number of words in the vocabulary. Next, there are N lines, each of
  23.      them containing one word of the vocabulary. A word is a string having characters
  24.      in the range 'a' - 'z'. The length of every word is at most 20 characters.
  25.      The value of L does not exceed 5, the value of N does not exceed 5000.
  26.  
  27.      Output
  28.      The output contains one text line with single integer representing the maximal
  29.      length of optimal sequences of transformations.
  30.      */
  31.     public static int countOfEditOneTrans; //L - neni vyresena kontrola
  32.     public static int countOfWords; //N
  33.     public static String[] vocabulary;
  34.     public static int[] dist;
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         //args = new String[]{"pub10.in"};
  38.         BufferedReader br = (args.length == 0)
  39.                 ? new BufferedReader(new InputStreamReader(System.in))
  40.                 : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
  41.  
  42.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  43.         countOfEditOneTrans = Integer.parseInt(configuration.nextToken());
  44.         countOfWords = Integer.parseInt(configuration.nextToken());
  45.         vocabulary = new String[countOfWords];
  46.         for (int i = 0; i < countOfWords; i++) {
  47.             vocabulary[i] = br.readLine();
  48.         }
  49.         int optimal = findOptimal();
  50.         System.out.println(optimal);
  51.  
  52.     }
  53.  
  54.     private static int computeDTW(String base, String compare) {
  55.         int bLength = base.length() + 1;
  56.         int cLength = compare.length() + 1;
  57.         int[] DTWdist = new int[bLength]; //ceny transofmraci
  58.         int[] DTWdisthelp = new int[bLength];
  59.         int match; //shoda
  60.         int insertion; //vlozeni
  61.         int deletion; //smazani
  62.         int replacement; //nahrazeni
  63.         int[] temp;
  64.         int count;
  65.         //
  66.         for (int b = 0; b < bLength; b++) {
  67.             DTWdist[b] = b;
  68.         }
  69.         for (int c = 1; c < cLength; c++) {
  70.             DTWdist[0] = c; //na zacatek nula, proto od indexu 1 a delky jsou +1
  71.             count = 0;
  72.             for (int b = 1; b < bLength; b++) {
  73.                 //pismenka se se shoduji
  74.                 if (base.charAt(b - 1) == compare.charAt(c - 1)) {
  75.                     match = 0;
  76.                 } else {
  77.                     match = 1;
  78.                 }
  79.                 //vypocty pro jednotlive operace - pricteme jim +1 a vybereme tu
  80.                 //nejmensi
  81.                 //kontrola L... ale je to blbost
  82.                 if (count < countOfEditOneTrans) {
  83.                     replacement = DTWdist[b - 1] + match;
  84.                     insertion = DTWdist[b] + 1;
  85.                     deletion = DTWdisthelp[b - 1] + 1;
  86.                     DTWdisthelp[b] = minimumCost(insertion, deletion, replacement);
  87.                     count++;
  88.                 }
  89.             }
  90.             temp = DTWdist;
  91.             DTWdist = DTWdisthelp;
  92.             DTWdisthelp = temp;
  93.         }
  94.         return DTWdist[bLength - 1];
  95.     }
  96.  
  97.     private static int findOptimal() {
  98.         int[] distances = new int[countOfWords - 1];
  99.         int sortedDistances[] = new int[countOfWords - 1];
  100.         int[] higherDistances = new int[countOfWords - 1];
  101.         for (int i = 0; i < countOfWords - 1; i++) {
  102.             for (int j = i + 1; j < countOfWords; j++) {
  103.                 distances[i] = computeDTW(vocabulary[i], vocabulary[j]);
  104.             }
  105.             sortedDistances = countingSort(distances);
  106.             higherDistances[i] = sortedDistances[countOfWords - 2];
  107.         }
  108.         higherDistances = countingSort(higherDistances);
  109.         return higherDistances[countOfWords - 2];
  110.     }
  111.  
  112.     private static int minimumCost(int a, int b, int c) {
  113.         return Math.min(Math.min(a, b), c);
  114.     }
  115.  
  116.     public static int[] countingSort(int[] array) {
  117.         int[] sortedArray = new int[countOfWords - 1];
  118.         int min = array[0];
  119.         int max = array[0];
  120.         for (int i = 1; i < countOfWords - 1; i++) {
  121.             if (array[i] < min) {
  122.                 min = array[i];
  123.             } else if (array[i] > max) {
  124.                 max = array[i];
  125.             }
  126.         }
  127.         //histogram length from min to max
  128.         int[] hist = new int[max - min + 1];
  129.         for (int i = 0; i < countOfWords - 1; i++) {
  130.             hist[array[i] - min]++;
  131.         }
  132.         //pole vyskytu
  133.         hist[0]--;
  134.         for (int i = 1; i < hist.length; i++) {
  135.             hist[i] = hist[i] + hist[i - 1];
  136.         }
  137.         //min to max
  138.         for (int i = countOfWords - 2; i >= 0; i--) {
  139.             int histIndex = array[i] - min;
  140.             int index = hist[histIndex];
  141.             sortedArray[index] = array[i];
  142.             hist[histIndex]--;
  143.         }
  144.         return sortedArray;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement