Finite state machine for searching "ABBA"

I am trying to write a random short-circuit case code to simulate a finite state machine that looks for a string from As and Bs to see if the string "ABBA" is present. When I enter only "ABBA", it outputs Word found!as intended. However, if I enter "AABBA", it does not find the word and displays the correct message. Any help is appreciated. Thank!

import java.util.*;
public class AB{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        String word = input.next();
        int current = 0;
        int status = 0;
        System.out.println("Starting to evaluate...");
        while(status != 4){
            for(int i = current; i < word.length(); i++){
                String part = word.substring(current, i + 1);
                switch(status){
                    case 0: //start
                        if(part.equals("A")){
                            status = 1;
                        }
                        else if(part.equals("B"){
                            status = 0; 
                            current = i;
                        }
                    break;
                    case 1: //A is there now
                        if(part.equals("AB")){
                            status = 2;
                        }
                        else if(part.equals("AA"){
                            status = 1;
                            current = 1;
                        }
                    break;
                    case 2: //AB is there now
                        if(part.equals("ABB")){
                            status = 3;
                        }
                        else if(part.equals("ABA"){
                            status = 1;
                            current = 1;
                        }
                    break;
                    case 3: //ABB is there now
                        if(part.equals("ABBA")){
                            status = 4;
                            System.out.println("Word found!");
                        }
                        else if(part.equals("ABBB"){
                            status = 0;
                            current = i;
                        }
                    break;
                }
            }
        }
    }
}
+4
source share
2 answers

, , , . , , . . , , . .

:

enter image description here

, :

public boolean abbaMatcher(String abba)
{
    int state = 0;
    int symbol = 0;


    while (symbol < abba.length()){
        char c = abba.charAt(symbol);
        switch (state){
            case 0: if(c == 'a'){
                        state = 1;
                    }else{
                        state = 0;
                    };
                    break;
            case 1: if(c == 'b'){
                        state = 2;
                    }else{
                        state = 1;
                    };
                    break;
            case 2: if(c == 'b'){
                        state = 3;
                    }else{
                        state = 1;
                    };
                    break;
            case 3: if(c == 'a'){
                        return true;
                    }else{
                        state = 0;
                    };
                    break;
        }
        symbol++;
    }

    return false;
}

for, while/switch .

+3

, ( ) , , .

, , : "ABAB" "ABABAB" "ABAB", 0 "ABAB", 2. , wIndex = startIndex.

:

public static void main (String[] args) throws java.lang.Exception {
    // Scanner input = new Scanner(System.in);
    String word = "B BABBABBA B";
    String seq = "ABBA";
    char[] wChars = word.toCharArray();
    char[] sChars = seq.toCharArray();
    int wIndex = 0; // wChars index
    int sIndex = 0; // sChars index
    int startIndex = 0; // starting index of the seq found in wChars

    System.out.println("Starting to evaluate...");

    while(wIndex < wChars.length) {
        if(wChars[wIndex] == sChars[sIndex]) {
            if(sIndex == 0) {
                startIndex = wIndex;
            }
            sIndex += 1;
        } else {
            sIndex = 0;
        }

        if(sIndex >= sChars.length) {
            System.out.println("Sequence \"" + seq + "\" found at index " + startIndex + ".");
            sIndex = 0;
            wIndex = startIndex; // set wIndex to startIndex to account for
                                 // sequence within a sequence, basically
                                 // backtracking
        }

        wIndex += 1;
    }
}

:

Starting to evaluate...
Sequence "ABBA" found at index 3.
Sequence "ABBA" found at index 6.
0

Source: https://habr.com/ru/post/1682513/


All Articles