Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package aizlab13;
- /**
- *
- * @author Hubert
- */
- public class Wzorzec13lab {
- public boolean czyŁatwy(String wzorzec){
- int dlugosc=wzorzec.length();
- for(int i=0;i<=(dlugosc/3);i++){
- for(int j=0; j<=i;j++){
- if(wzorzec.charAt(j)==wzorzec.charAt(j+(i+1))&& wzorzec.charAt(j)==wzorzec.charAt(j+(2*(i+1)))){
- return false;
- }//if
- }//for2
- }//for1
- return true;
- }
- public void GS(String wzorzec, String tekst) {
- int ile = 0;
- int n = tekst.length();
- int m = wzorzec.length();
- System.out.println("-------------------------------------------");
- System.out.println("Indeksy wystąpien wzorca: ");
- int i = 1;
- while (i <= n - m + 1) {
- int j = 0;
- while ((j < m) && (wzorzec.charAt(j) == tekst.charAt(i + j - 1))) {
- j++;
- }
- if (j == m) {
- System.out.println(i);
- ile++;
- }
- i = (int) (i + Math.max(1, Math.ceil(j / 3)));
- }
- System.out.println("ilość wystąpień: " + ile);
- }
- public void KMP(String wzorzec, String tekst) {
- int ile = 0;
- int n = tekst.length();
- int m = wzorzec.length();
- int P[] = new int[100];
- P[0] = 0;
- P[1] = 0;
- int t = 0;
- System.out.println("-------------------------------------------");
- System.out.println("Indeksy wystąpien wzorca: ");
- for (int j = 2; j <= m; j++) {
- while ((t > 0) && (wzorzec.charAt(t) != wzorzec.charAt(j - 1))) {
- t = P[t];
- }
- if (wzorzec.charAt(t) == wzorzec.charAt(j - 1)) {
- t++;
- }
- P[j] = t;
- }
- int i = 1;
- int j = 0;
- while (i <= n - m + 1) {
- j = P[j];
- while ((j < m) && (wzorzec.charAt(j) == tekst.charAt(i + j - 1))) {
- j++;
- }
- if (j == m) {
- System.out.println(i);
- ile++;
- }
- i = i + Math.max(1, j - P[j]);
- }
- System.out.println("ilość wystąpień: " + ile);
- }
- public void bm(String wzorzec, String tekst) {
- int[] alf = new int[26];
- int ile=0;
- int n = tekst.length();
- int m = wzorzec.length();
- System.out.println("-------------------------------------------");
- System.out.println("Indeksy wystąpien wzorca: ");
- if (n >= m) {
- for (int i = 0; i < alf.length; i++) {
- alf[i] = -1;
- }
- for(int i = 0; i < m; i++) {
- alf[(int)wzorzec.charAt(i) - (int)'a'] = i;
- }
- int pp = 0;
- int i = 0;
- int j = 0;
- while(i <= n-m) {
- j = m-1;
- while((j>-1) && (wzorzec.charAt(j) == tekst.charAt(i+j))) {
- j--;
- }
- if(j <= -1) {
- pp = i;
- System.out.println(pp);
- i++;
- ile++;
- } else {
- i += Math.max(1, j-alf[((int)tekst.charAt(i+j)) - (int)'a']);
- }
- }
- }
- System.out.println("ilość wystąpień: " + ile);
- }
- }
- //main
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package aizlab13;
- /**
- *
- * @author Hubert
- */
- public class Aizlab13 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Wzorzec13lab w =new Wzorzec13lab();
- if(w.czyŁatwy("abcdef")==true){
- System.out.println("Łatwy");
- }else{
- System.out.println("Nie łatwy");
- }
- w.GS("asd","asdbdasdbdasdbd");
- w.KMP("bd","asdbdasdbdasdbd");
- w.bm("bd","asdbdasdbdasdbd");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement