Advertisement
Narintep

Untitled

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