Advertisement
Alisator

leven

Jan 23rd, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 KB | None | 0 0
  1. package textsearch;
  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 max = 0;
  41.    
  42.     public static HashMap<Node, Set<Node>> LevGraph;
  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.         for (int i = 0; i < countOfWords; i++) {
  56.             add(new Node(br.readLine()));
  57.         }
  58.         //  printGraph();
  59.         for (Node n : LevGraph.keySet()) {
  60.             if (n.depth == -1) {
  61.                 n.depth = 0;
  62.                 BFS(n);
  63.             }
  64.         }
  65.        
  66.         System.out.println(max);
  67.     }
  68.    
  69.     private static int computeDTW(Node base, Node compare) {
  70.         int bLength = base.word.length() + 1;
  71.         int cLength = compare.word.length() + 1;
  72.         int[] DTWdist = new int[bLength]; //ceny transofmraci
  73.         int[] DTWdisthelp = new int[bLength];
  74.         int match; //shoda
  75.         int insertion; //vlozeni
  76.         int deletion; //smazani
  77.         int replacement; //nahrazeni
  78.         int[] temp;
  79.        
  80.         for (int b = 0; b < bLength; b++) {
  81.             DTWdist[b] = b;
  82.         }
  83.         for (int c = 1; c < cLength; c++) {
  84.             DTWdisthelp[0] = c; //na zacatek nula, proto od indexu 1 a delky jsou +1
  85.             for (int b = 1; b < bLength; b++) {
  86.                 //pismenka se se shoduji
  87.                 if (base.word.charAt(b - 1) == compare.word.charAt(c - 1)) {
  88.                     match = 0;
  89.                 } else {
  90.                     match = 1;
  91.                 }
  92.                 //vypocty pro jednotlive operace - pricteme jim +1 a vybereme tu
  93.                 insertion = DTWdist[b] + 1;
  94.                 replacement = DTWdist[b - 1] + match;
  95.                 deletion = DTWdisthelp[b - 1] + 1;
  96.                 DTWdisthelp[b] = minimumCost(insertion, deletion, replacement);
  97.             }
  98.             temp = DTWdist;
  99.             DTWdist = DTWdisthelp;
  100.             DTWdisthelp = temp;
  101.         }
  102.         return DTWdist[bLength - 1];
  103.     }
  104.    
  105.     private static int minimumCost(int a, int b, int c) {
  106.         return Math.min(Math.min(a, b), c);
  107.     }
  108.    
  109.     private static void add(Node n) {
  110.         int levDist;
  111.         if (LevGraph.containsKey(n) == false) {
  112.             LevGraph.put(n, new HashSet<Node>());
  113.         }
  114.         for (Node s : LevGraph.keySet()) {
  115.             //splnena podminka poctu operaci, udelame propojeni
  116.             levDist = computeDTW(n, s);
  117.             if (n.equals(s) == false && levDist <= countOfEditOneTrans) {
  118.                 if (!LevGraph.containsKey(s)) {
  119.                     LevGraph.put(s, new HashSet<Node>());
  120.                 }
  121.                 LevGraph.get(n).add(s);
  122.                 LevGraph.get(s).add(n);
  123.             }
  124.         }
  125.     }
  126.    
  127.     private static void BFS(Node root) {
  128.         Queue<Node> q = new LinkedList<Node>();
  129.         q.add(root);
  130.         while (!q.isEmpty()) {
  131.             Node t = q.remove();
  132.             if (t.depth > max) {
  133.                 max = t.depth;
  134.             }
  135.             for (Node s : LevGraph.get(t)) {
  136.                 if (s.depth == -1) {
  137.                     s.depth = t.depth + 1;
  138.                     q.add(s);
  139.                 }
  140.             }
  141.         }
  142.         clearDepth();
  143.     }
  144.    
  145.     private static void clearDepth() {
  146.         for (Node n : LevGraph.keySet()) {
  147.             n.depth = -1;
  148.         }
  149.     }
  150.  
  151.     //nonused
  152.     private static void printGraph() {
  153.         for (Entry<Node, Set<Node>> entry : LevGraph.entrySet()) {
  154.             System.out.println(entry.getKey() + ": " + entry.getValue().toString());
  155.         }
  156.     }
  157.    
  158.     private static class Node {
  159.        
  160.         int depth;
  161.         String word;
  162.        
  163.         public Node(String word, int depth) {
  164.             this.depth = depth;
  165.             this.word = word;
  166.         }
  167.        
  168.         public Node(String word) {
  169.             this.depth = -1;
  170.             this.word = word;
  171.         }
  172.        
  173.         @Override
  174.         public String toString() {
  175.             return "word " + this.word + "- " + this.depth;
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement