Advertisement
Narintep

Untitled

Mar 15th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.89 KB | None | 0 0
  1.  
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) throws FileNotFoundException {
  10.         int input = 0;
  11.         if (args.length == 0)
  12.             input = 10;
  13.         else input = Integer.parseInt(args[0]);
  14.  
  15.         String Direct = System.getProperty("user.dir");
  16.         if (Direct.indexOf("src") == -1)
  17.             Direct += "\\src";
  18.         String files = Direct + "\\product.txt";
  19.         Scanner file = new Scanner(new File(files));
  20.         ArrayList<String> list = new ArrayList<>();
  21.         while (file.hasNextLine()) {
  22.             list.add(file.nextLine());
  23.         }
  24.         file.close();
  25.  
  26.         Scanner key = new Scanner(System.in);
  27.         System.out.println("Product Search - Input your keyword (s):");
  28.         String name = key.nextLine();
  29.         String[] parts = name.split(" ");
  30.  
  31.         ArrayList<data> Keep = new ArrayList<>();
  32.         for (int j = 0; j < list.size(); j++) {
  33.             data Store = new data(list.get(j));
  34.             ArrayList<Integer> Index = new ArrayList<Integer>();
  35.             for (int k = 0; k < parts.length; k++) {
  36.                 if (findBoyerMoore(list.get(j).toUpperCase().toCharArray(), parts[k].toUpperCase().toCharArray()) != -1) {
  37.                     Index.add(findBoyerMoore(list.get(j).toUpperCase().toCharArray(), parts[k].toUpperCase().toCharArray()));
  38.                     if (k == 0) {
  39.                         Store.firstindex = findBoyerMoore(list.get(j).toUpperCase().toCharArray(), parts[k].toUpperCase().toCharArray());
  40.                         Store.count++;
  41.  
  42.                     }
  43.                 }
  44.             }
  45.             Integer min = Integer.MAX_VALUE;
  46.             for (int l = 0; l < Index.size(); l++) {
  47.                 for (int m = l + 1; m < Index.size(); m++) {
  48.                     if (Math.abs((Index.get(l) - Index.get(m))) < min) {
  49.                         min = (Math.abs((Index.get(l) - Index.get(m))));
  50.                     }
  51.                 }
  52.                 Store.mDistance = min;
  53.             }
  54.  
  55.             Keep.add(Store);
  56.            // System.out.println(Store);
  57.         }
  58.         List<data> S = RadixSort(Keep);
  59.         for (int i = 0; i < S.size(); i++) {
  60.             if(S.get(i).count!=0)
  61.             System.out.println(S.get(i).name);
  62.         }
  63.     }
  64.  
  65.         public static class data {
  66.             String name;
  67.  
  68.             int firstindex, mDistance, Index, count;
  69.  
  70.             public data(String name) {
  71.                 this.name = name;
  72.                 count = 0;
  73.                 firstindex = Integer.MAX_VALUE;
  74.                 mDistance = Integer.MAX_VALUE;
  75.             }
  76.  
  77.             @Override
  78.             public String toString() {
  79.                 return name + " " + count + " " + firstindex + " " + mDistance;
  80.             }
  81.         }
  82.  
  83.         public static List<data> RadixSort(ArrayList < data > rData)
  84.         {
  85.             Integer max_count = 0, max_position = 0, max_minDist = 0;
  86.             for (int i = 0; i < rData.size(); i++) {
  87.                 if (rData.get(i).firstindex > max_count)
  88.                     max_count = rData.get(i).firstindex;
  89.                 if (rData.get(i).count > max_position)
  90.                     max_position = rData.get(i).count;
  91.                 if (rData.get(i).mDistance > max_minDist)
  92.                     max_minDist = rData.get(i).mDistance;
  93.             }
  94.             int digit_count = 0, digit_position = 0, digit_dist = 0;
  95.             while (max_count > 0) {
  96.                 max_count = max_count / 10;
  97.                 digit_count++;
  98.             }
  99.             while (max_position > 0) {
  100.                 max_position = max_position / 10;
  101.                 digit_position++;
  102.             }
  103.             while (max_minDist > 0) {
  104.                 max_minDist = max_minDist / 10;
  105.                 digit_dist++;
  106.             }
  107.             LinkedList<data> Bucket1 = new LinkedList<>();
  108.             for (int i = 0; i < rData.size(); i++)
  109.             {
  110.                 Bucket1.add(rData.get(i));
  111.             }
  112.             int d = 1;
  113.             for (int i = 1; i <= digit_dist; i++) {
  114.                 LinkedList<data> tmp = new LinkedList<>();
  115.                 for (int j = 0; j < 10; j++) {
  116.                     for (int k = 0; k < Bucket1.size(); k++) {
  117.                         if ((Bucket1.get(k).mDistance / d) % 10 == j)
  118.                             tmp.add(Bucket1.get(k));
  119.                     }
  120.                 }
  121.                 Bucket1 = new LinkedList<>();
  122.                 for (int j = 0; j < tmp.size(); j++) {
  123.                     Bucket1.add(tmp.get(j));
  124.                 }
  125.                 d *= 10;
  126.             }
  127.             d = 1;
  128.             for (int i = 1; i <= digit_position; i++) {
  129.                 LinkedList<data> tmp = new LinkedList<>();
  130.                 for (int j = 0; j < 10; j++) {
  131.                     for (int k = 0; k < Bucket1.size(); k++) {
  132.                         if ((Bucket1.get(k).firstindex / d) % 10 == j)
  133.                             tmp.add(Bucket1.get(k));
  134.                     }
  135.                 }
  136.                 Bucket1 = new LinkedList<>();
  137.                 for (int j = 0; j < tmp.size(); j++) {
  138.                     Bucket1.add(tmp.get(j));
  139.                 }
  140.                 d *= 10;
  141.             }
  142.             d = 1;
  143.             for (int i = 1; i <= digit_count; i++) {
  144.                 LinkedList<data> tmp = new LinkedList<>();
  145.                 for (int j = 9; j >= 0; j--) {
  146.                     for (int k = 0; k < Bucket1.size(); k++) {
  147.                         if ((Bucket1.get(k).count / d) % 10 == j)
  148.                             tmp.add(Bucket1.get(k));
  149.                     }
  150.                 }
  151.                 Bucket1 = new LinkedList<>();
  152.                 for (int j = 0; j < tmp.size(); j++) {
  153.                     Bucket1.add(tmp.get(j));
  154.                 }
  155.                 d *= 10;
  156.             }
  157.             return Bucket1;
  158.         }
  159.  
  160.     public static int findBoyerMoore(char[] text, char[] pattern) {
  161.         int n = text.length;
  162.         int m = pattern.length;
  163.         if (m == 0) return 0;
  164.         Map<Character, Integer> last = new HashMap<>(); // the 'last' map
  165.         for (int i = 0; i < n; i++)
  166.             last.put(text[i], -1); // set −1 as default for all text characters
  167.         for (int k = 0; k < m; k++) {
  168.             last.put(pattern[k], k);
  169.         }
  170.         int i = m - 1;
  171.         int k = m - 1;
  172.         while (i < n) {
  173.             if (text[i] == pattern[k]) { // a matching character
  174.                 if (k == 0) return i; // entire pattern has been found
  175.                 i--; // otherwise, examine previous
  176.                 k--; // characters of text/pattern
  177.             } else {
  178.                 i += m - Math.min(k, 1 + last.get(text[i])); // case analysis for jump step
  179.                 k = m - 1;
  180.                 // restart at end of pattern
  181.             }
  182.         }
  183.         return -1;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement