Advertisement
Alisator

pokus_BFS

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