Advertisement
nazar_art

SearchPhrase - 01 variant

Jan 22nd, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package task;
  2.  
  3. import java.io.*;
  4.  
  5. class SearchPhrase {
  6.  
  7.     // walk to root way
  8.     public void walk(String path) throws IOException {
  9.  
  10.         File root = new File(path);
  11.         File[] list = root.listFiles();
  12.         for (File titleName : list) {
  13.             if (titleName.isDirectory()) {
  14.                 walk(titleName.getAbsolutePath());
  15.                 // System.out.println( "Dir:" + titleName.getAbsoluteFile() );
  16.             } else {
  17.                 System.out.println("File:" + titleName.getAbsoluteFile());
  18.             }
  19.         }
  20.     }
  21.  
  22.     // Read file as one line
  23.     public static String read(String fileName) {
  24.         StringBuilder strBuider = new StringBuilder();
  25.         try {
  26.             BufferedReader in = new BufferedReader(new FileReader(new File(
  27.                     fileName).getAbsoluteFile()));
  28.             String strInput;
  29.             while ((strInput = in.readLine()) != null) {
  30.                 strBuider.append(strInput);
  31.                 strBuider.append("\n");
  32.             }
  33.  
  34.             in.close();
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         }
  38.  
  39.         return strBuider.toString();
  40.     }
  41.  
  42.     public static int searchPhrase(String fileName, String what) {
  43.         int n = fileName.length(); // Длина строки, в которой происходит поиск
  44.         int m = what.length(); // Длина подстроки
  45.  
  46.         // Формирование таблицы сдвигов
  47.         int[] table = new int[m];
  48.         table[0] = 0;
  49.         int shift = 0;
  50.         for (int q = 1; q < m; q++) {
  51.             while (shift > 0 && what.charAt(shift) != what.charAt(q)) {
  52.                 shift = table[shift - 1];
  53.             }
  54.             if (what.charAt(shift) == what.charAt(q))
  55.                 shift++;
  56.             table[q] = shift;
  57.         }
  58.  
  59.         // Поиск с использованием таблицы сдвигов
  60.         shift = 0;
  61.         for (int i = 0; i < n; i++) {
  62.             while (shift > 0 && what.charAt(shift) != fileName.charAt(i)) {
  63.                 shift = table[shift - 1];
  64.             }
  65.             if (what.charAt(shift) == fileName.charAt(i))
  66.                 shift++;
  67.             if (shift == m)
  68.                 return i - m + 1; // подстрока найдена
  69.         }
  70.         return -1; // подстрока не найдена
  71.     }
  72.  
  73.     /**
  74.      * Тестовая процедура: запускает алгоритм и выводит результат его работы.
  75.      *
  76.      * @param args
  77.      */
  78.     public static void main(String[] args) {
  79.         SearchPhrase example = new SearchPhrase();
  80.         String file = read("program test.txt");
  81.         int ndx = example.searchPhrase(file, "программист");
  82.  
  83.         if (ndx >= 0) {
  84.             System.out.println("Index found: " + ndx);
  85.         } else {
  86.             System.out.println("Substring not found");
  87.         }
  88.         try {
  89.             example.walk("C:\\Documents and Settings\\User\\Java Hangman\\Java\\Anton");
  90.         } catch (IOException e) {
  91.             e.printStackTrace();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement