Advertisement
kamasazi99

aiz 13 lab

Jan 19th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package aizlab13;
  7.  
  8. /**
  9.  *
  10.  * @author Hubert
  11.  */
  12. public class Wzorzec13lab {
  13.  
  14.  public boolean czyŁatwy(String wzorzec){
  15.  
  16.         int dlugosc=wzorzec.length();
  17.         for(int i=0;i<=(dlugosc/3);i++){
  18.             for(int j=0; j<=i;j++){
  19.                 if(wzorzec.charAt(j)==wzorzec.charAt(j+(i+1))&& wzorzec.charAt(j)==wzorzec.charAt(j+(2*(i+1)))){
  20.                     return false;
  21.                 }//if
  22.             }//for2
  23.         }//for1
  24.  
  25.         return true;
  26.     }
  27.  public void GS(String wzorzec, String tekst) {
  28.         int ile = 0;
  29.         int n = tekst.length();
  30.         int m = wzorzec.length();
  31.         System.out.println("-------------------------------------------");
  32.         System.out.println("Indeksy wystąpien wzorca: ");
  33.         int i = 1;
  34.         while (i <= n - m + 1) {
  35.             int j = 0;
  36.             while ((j < m) && (wzorzec.charAt(j) == tekst.charAt(i + j - 1))) {
  37.                 j++;
  38.  
  39.             }
  40.             if (j == m) {
  41.                 System.out.println(i);
  42.                 ile++;
  43.             }
  44.             i = (int) (i + Math.max(1, Math.ceil(j / 3)));
  45.  
  46.         }
  47.         System.out.println("ilość wystąpień: " + ile);
  48.     }
  49.  
  50. public void KMP(String wzorzec, String tekst) {
  51.         int ile = 0;
  52.         int n = tekst.length();
  53.         int m = wzorzec.length();
  54.         int P[] = new int[100];
  55.         P[0] = 0;
  56.         P[1] = 0;
  57.         int t = 0;
  58.         System.out.println("-------------------------------------------");
  59.         System.out.println("Indeksy wystąpien wzorca: ");
  60.         for (int j = 2; j <= m; j++) {
  61.             while ((t > 0) && (wzorzec.charAt(t) != wzorzec.charAt(j - 1))) {
  62.                 t = P[t];
  63.             }
  64.             if (wzorzec.charAt(t) == wzorzec.charAt(j - 1)) {
  65.                 t++;
  66.             }
  67.             P[j] = t;
  68.         }
  69.         int i = 1;
  70.         int j = 0;
  71.         while (i <= n - m + 1) {
  72.             j = P[j];
  73.             while ((j < m) && (wzorzec.charAt(j) == tekst.charAt(i + j - 1))) {
  74.                 j++;
  75.             }
  76.             if (j == m) {
  77.                 System.out.println(i);
  78.                 ile++;
  79.             }
  80.             i = i + Math.max(1, j - P[j]);
  81.         }
  82.         System.out.println("ilość wystąpień: " + ile);
  83.     }
  84. public void bm(String wzorzec, String tekst) {
  85.         int[] alf = new int[26];
  86.         int ile=0;
  87.         int n = tekst.length();
  88.         int m = wzorzec.length();
  89.         System.out.println("-------------------------------------------");
  90.         System.out.println("Indeksy wystąpien wzorca: ");
  91.         if (n >= m) {
  92.             for (int i = 0; i < alf.length; i++) {
  93.                 alf[i] = -1;
  94.             }
  95.  
  96.             for(int i = 0; i < m; i++) {
  97.                 alf[(int)wzorzec.charAt(i) - (int)'a'] = i;
  98.             }
  99.  
  100.             int pp = 0;
  101.             int i = 0;
  102.             int j = 0;
  103.  
  104.             while(i <= n-m) {
  105.                 j = m-1;
  106.                 while((j>-1) && (wzorzec.charAt(j) == tekst.charAt(i+j))) {
  107.                     j--;
  108.                 }
  109.                 if(j <= -1) {
  110.                     pp = i;
  111.                     System.out.println(pp);
  112.                     i++;
  113.                     ile++;
  114.                 } else {
  115.                     i += Math.max(1, j-alf[((int)tekst.charAt(i+j)) - (int)'a']);
  116.                 }
  117.             }
  118.         }
  119.         System.out.println("ilość wystąpień: " + ile);
  120.     }
  121.    
  122. }
  123.  
  124.  
  125. //main
  126.  
  127.  
  128. /*
  129.  * To change this license header, choose License Headers in Project Properties.
  130.  * To change this template file, choose Tools | Templates
  131.  * and open the template in the editor.
  132.  */
  133. package aizlab13;
  134.  
  135. /**
  136.  *
  137.  * @author Hubert
  138.  */
  139. public class Aizlab13 {
  140.  
  141.     /**
  142.      * @param args the command line arguments
  143.      */
  144.     public static void main(String[] args) {
  145.       Wzorzec13lab w =new Wzorzec13lab();
  146.  
  147.       if(w.czyŁatwy("abcdef")==true){
  148.             System.out.println("Łatwy");
  149.         }else{
  150.             System.out.println("Nie łatwy");
  151.         }
  152.       w.GS("asd","asdbdasdbdasdbd");
  153.  
  154.       w.KMP("bd","asdbdasdbdasdbd");
  155.      w.bm("bd","asdbdasdbdasdbd");
  156.     }
  157.    
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement