Advertisement
Alisator

leven2

Jan 23rd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package pal;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16.  *
  17.  * @author pc404
  18.  */
  19. public class SearchText {
  20.  
  21.     private int N;
  22.     private int M;
  23.     private int cI;
  24.     private int cD;
  25.     private int cR;
  26.     private String P;
  27.     private String T;
  28.     private String actStr;
  29.     private char actChar;
  30.     BufferedReader reader;
  31.     private String line;
  32.     private String[] spLine;
  33.     private int index, endLength, dist;
  34.     private int[][] levenstein;
  35.     private char a;
  36.     private int min;
  37.     private int act;
  38.     private boolean isUnderMin;
  39.  
  40.     public SearchText() {
  41.         try {
  42.             // TODO code application logic here
  43.             reader = new BufferedReader(new InputStreamReader(System.in));
  44.             //reader = new BufferedReader(new FileReader("pub02.in"));
  45.             line = reader.readLine();
  46.             spLine = line.split(" ");
  47.             N = Integer.parseInt(spLine[0]);
  48.             M = Integer.parseInt(spLine[1]);
  49.             cI = Integer.parseInt(spLine[2]);
  50.             cD = Integer.parseInt(spLine[3]);
  51.             cR = Integer.parseInt(spLine[4]);
  52.             T = reader.readLine();
  53.             P = reader.readLine();
  54.             index = -1;
  55.             endLength = -1;
  56.             dist = Integer.MAX_VALUE;
  57.             levenstein = new int[N + 1][M + 1];
  58.             for (int i = 0; i < levenstein.length; i++) {
  59.                 levenstein[i][0] = i * cI;
  60.             }
  61.             for (int k = 1; k < levenstein[0].length; k++) {
  62.                 levenstein[0][k] = k * cD;
  63.             }
  64.             for (int j = 0; j < N; j++) {
  65.                 for (int i = 1; i <= N - j; i++) {
  66.                     actChar = T.charAt(i + j - 1);
  67.                     countDistance(actChar, i);
  68.                     if (isUnderMin == false) {
  69.                         break;
  70.                     }
  71.                     act = levenstein[i][M];
  72.                     if (act < dist) {
  73.                         dist = act;
  74.                         index = j;
  75.                         endLength = i;
  76.                     } else if (act == dist) {
  77.                         if (i < endLength) {
  78.                             index = j;
  79.                             endLength = i;
  80.                         } else if (i == endLength) {
  81.                             if (j < index) {
  82.                                 index = j;
  83.                             }
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88.             System.out.println(index + " " + endLength + " " + dist);
  89.         } catch (IOException ioe) {
  90.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ioe);
  91.         }
  92.     }
  93.  
  94.     private void countDistance(char a, int i) {
  95.         isUnderMin = false;
  96.         for (int j = 1; j <= M; j++) {
  97.             if (a == P.charAt(j - 1)) {
  98.                 min = levenstein[i - 1][j - 1];
  99.             } else {
  100.                 min = levenstein[i - 1][j - 1] + cR;
  101.             }
  102.             if (levenstein[i][j - 1] + cD < min) {
  103.                 min = levenstein[i][j - 1] + cD;
  104.             }
  105.             if (levenstein[i - 1][j] + cI < min) {
  106.                 min = levenstein[i - 1][j] + cI;
  107.             }
  108.             levenstein[i][j] = min;
  109.             if (levenstein[i][j] < dist) {
  110.                 isUnderMin = true;
  111.             }
  112.             //System.out.print(levenstein[i][j] + " ");
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement