zoukankan      html  css  js  c++  java
  • 有限自动机的构造与识别

    #include "iostream.h"
    #include "string.h" 
    #include "fstream.h"
    #define NULL 0


    class TransTile
    {
    public:
    char current;
    char next;
    char input;
    TransTile(char C,char I,char Ne){
    current = C;
    next = Ne;
    input = I;
    }
    };

    class DFA
    {
    public:

    string States;
    char startStates;
    string finalStates;
    string Alphabets;
    vector <TransTile> Tile;

    DFA(){
    init();
    }
    void init()
    {
    cout << "输入有限状态集S:" << endl;
    cin >> States;
    cout << "输入字符集A:" << endl;
    cin >> Alphabets;
    cout << "输入状态转换式(格式为:状态-输入字符-下一状态,输入#结束):" << endl;
    cout << "例如:1a1 1a0 2a1 #" << endl;
    int h = 0;

    //while (cin>>input){
    // TransTile transval(input[0], input[1], input[2]);
    // Tile.push_back(transval);
    //}
    while(true){
    char input[4];
    cin>>input;
    if(strcmp(input,"#")==0)
    break;
    TransTile transval(input[0],input[1],input[2]);
    Tile.push_back(transval);
    }
    cout << "输入初态:" << endl;
    cin >> startStates;
    cout << "输入终态:" << endl;
    cin >> finalStates;
    }

    char move(char P,char I){
    for (int i = 0; i < Tile.size(); i++){
    if (Tile[i].current == P&&Tile[i].input == I){
    return Tile[i].next;

    }
    return 'E';
    }

    void recognition(){
    string str;
    cout << "please input string:" << endl;
    cin >> str;
    int i = 0;
    char current = startStates;
    while (i < str.length()){
    current = move(current, str[i]);
    if (current == 'E'){
    break;
    }
    i++;
    }
    if (finalStates.find(current) != finalStates.npos){
    cout << "ERROR!" << endl;

    }
    else
    {
    cout << "ERROR!" << endl;
    }
    }
    };

    int main(){
    DFA dfa; 
    bool tag;

    while(1){
    cout<<"continue to '1',else to '0':"<<endl;
    cin>>tag;
    if(tag){
    dfa.recognition();
    }else
    break;

    }
    return 0;
    }

  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/chenkaiqi/p/5017281.html
Copyright © 2011-2022 走看看