Advertisement
Guest User

beci pacer

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  9.         String[]parts= buff.readLine().split(" ");
  10.         int zbir=0;
  11.         List<String>lista1= new LinkedList<String>();
  12.         List<String>lista2= new LinkedList<String>();
  13.         List<String>lista3= new LinkedList<String>();
  14.  
  15.         String txt = buff.readLine();
  16.         Set<String> set = new HashSet<>();
  17.  
  18.  
  19.         for (int i = 0; i < Integer.parseInt(parts[0]); i++) {
  20.             zbir+=search(txt.toCharArray(),buff.readLine().toCharArray());
  21.         }
  22.         for (int i = 0; i < Integer.parseInt(parts[1]); i++) {
  23.             zbir+=search(txt.toCharArray(),buff.readLine().toCharArray());
  24.         }
  25.         for (int i = 0; i < Integer.parseInt(parts[2]); i++) {
  26.             zbir+=search(txt.toCharArray(),buff.readLine().toCharArray());
  27.         }
  28.  
  29.  
  30.         System.out.println(zbir);
  31.     }
  32.  
  33.  
  34.     static int NO_OF_CHARS = 256;
  35.  
  36.     //A utility function to get maximum of two integers
  37.     static int max (int a, int b) { return (a > b)? a: b; }
  38.  
  39.     //The preprocessing function for Boyer Moore's
  40.     //bad character heuristic
  41.     static void badCharHeuristic( char []str, int size,int badchar[])
  42.     {
  43.         int i;
  44.  
  45.         // Initialize all occurrences as -1
  46.         for (i = 0; i < NO_OF_CHARS; i++)
  47.             badchar[i] = -1;
  48.  
  49.         // Fill the actual value of last occurrence
  50.         // of a character
  51.         for (i = 0; i < size; i++)
  52.             badchar[(int) str[i]] = i;
  53.     }
  54.  
  55.     /* A pattern searching function that uses Bad
  56.     Character Heuristic of Boyer Moore Algorithm */
  57.     static int search( char txt[],  char pat[])
  58.     {
  59.         int zbir=0;
  60.         int m = pat.length;
  61.         int n = txt.length;
  62.  
  63.         int badchar[] = new int[NO_OF_CHARS];
  64.  
  65.       /* Fill the bad character array by calling
  66.          the preprocessing function badCharHeuristic()
  67.          for given pattern */
  68.         badCharHeuristic(pat, m, badchar);
  69.  
  70.         int s = 0;  // s is shift of the pattern with
  71.         // respect to text
  72.         while(s <= (n - m))
  73.         {
  74.             int j = m-1;
  75.  
  76.           /* Keep reducing index j of pattern while
  77.              characters of pattern and text are
  78.              matching at this shift s */
  79.             while(j >= 0 && pat[j] == txt[s+j])
  80.                 j--;
  81.  
  82.           /* If the pattern is present at current
  83.              shift, then index j will become -1 after
  84.              the above loop */
  85.             if (j < 0)
  86.             {
  87.                 //System.out.println("Patterns occur at shift = " + s);
  88.                 zbir++;
  89.  
  90.               /* Shift the pattern so that the next
  91.                  character in text aligns with the last
  92.                  occurrence of it in pattern.
  93.                  The condition s+m < n is necessary for
  94.                  the case when pattern occurs at the end
  95.                  of text */
  96.                 s += (s+m < n)? m-badchar[txt[s+m]] : 1;
  97.  
  98.             }
  99.  
  100.             else
  101.               /* Shift the pattern so that the bad character
  102.                  in text aligns with the last occurrence of
  103.                  it in pattern. The max function is used to
  104.                  make sure that we get a positive shift.
  105.                  We may get a negative shift if the last
  106.                  occurrence  of bad character in pattern
  107.                  is on the right side of the current
  108.                  character. */
  109.                 s += max(1, j - badchar[txt[s+j]]);
  110.         }
  111.         return zbir;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement