zoukankan      html  css  js  c++  java
  • 简单有穷自动机

    
    

    一、实验目标
      
    1、掌握有穷状态自动机的概念;  
    2、掌握有穷状态自动机的存储及表示方法;
    3、掌握有穷状态自动机与正则式之间的关系。

    
    
    
    
    

    二、实验要求
      
    1、输入正规式; 

    
    

    2、构造该正规式的有穷状态自动机;

    
    

    3. 以五元组形式输出。

    三、代码如下:

    1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 void DFA(int, int, string);
     5 int main()
     6 {
     7     string s;
     8     cin >> s;
     9     DFA(0, 1, s);
    10     return 0;
    11 }
    12 
    13 void DFA(int x, int y, string s)//采用递归的方法
    14 {
    15     int i = 0;
    16     for (i = 0; i < s.length(); i++)//多处采用for是为了保证关键字的优先级
    17         if (s[i] == '|')//如果s[i]==‘|’,则将字符串s分隔开,如 'a|b'的话,将字符串分隔为s1=a;s2=b;并再次进行递归 ,递归结束的条件为s[i]!=’|‘
    18         {
    19             string s1(s, 0, i - 1);
    20             string s2(s, i + 1);
    21             DFA(x, y, s1); DFA(x, y, s2);
    22         }
    23     for (i = 0; i < s.length(); i++)//同上,递归结束条件为是s[i]!=’.‘
    24         if (s[i] == '.')
    25         {
    26             string s1(s, i - 1, 1);
    27             string s2(s, i + 1, 1);
    28             DFA(x, y + 1, s1); DFA(y + 1, y, s2);
    29         }
    30     for (i = 0; i < s.length(); i++)//同上,递归结束条件为s[i]!=’*‘
    31     {
    32         if (s[i] == '*')
    33         {
    34             string s1("~");
    35             string s2(s, i - 1, 1);
    36             DFA(x, y + 2, s1);
    37             DFA(y + 2, y + 2, s2);
    38             DFA(y + 2, y, s1);
    39         }
    40     }
    41     if (s.length() == 1)//确保最后不会输出整个字符串
    42         cout << "f(" << x << "," << s << ")=" << y << endl;
    43 }

  • 相关阅读:
    Codeforces467C George and Job
    Codeforces205E Little Elephant and Furik and RubikLittle Elephant and Furik and Rubik
    Codeforce205C Little Elephant and Interval
    51nod1829 函数
    51nod1574 排列转换
    nowcoder35B 小AA的数列
    Codeforce893E Counting Arrays
    gym101612 Consonant Fencity
    CodeForces559C Gerald and Giant Chess
    CodeForces456D A Lot of Games
  • 原文地址:https://www.cnblogs.com/dfq621/p/6187096.html
Copyright © 2011-2022 走看看