博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动机系列之一:自动机的模板代码及其demo
阅读量:3703 次
发布时间:2019-05-21

本文共 4434 字,大约阅读时间需要 14 分钟。

里面一个函数就是一个自动机,其中包括了正则的自动机,是否为数字的的自动机等

#include 
using namespace std;bool isNumber(string s){ enum InputType { INVALID, // 1. invalid input SIGN, // 2. '+' or '-' DIGIT, // 3. digits DOT, // 4. '.' EXP // 5. 'e' }; const int NUM_INPUT = 5; if(s == " ") return false; //get rid of space int start = s.find_first_not_of(" "); int end = s.find_last_not_of(" "); string ss = s.substr(start, end-start+1); int n = ss.size(); int state = 0; const int transitionTable[][NUM_INPUT] = { -1, 3, 1, 2, -1, -1, -1, 1, 4, 5, -1, -1, 4, -1, -1, -1, -1, 1, 2, -1, -1, -1, 4, -1, 5, -1, 6, 7, -1, -1, -1, -1, 7, -1, -1, -1, -1, 7, -1, -1, -1, -1, 7, -1, -1, }; for(int i = 0; i < n; ++i) { InputType type = INVALID; if(ss[i] == '+' || ss[i] == '-') { type = SIGN; } else if(isdigit(ss[i])) { type = DIGIT; } else if(ss[i] == '.') { type = DOT; } else if(ss[i] == 'e' || ss[i] == 'E') { type = EXP; } state = transitionTable[state][type]; if(state == -1) return false; } return state == 1 || state == 4 || state == 7;}bool isABAB(string s){ enum InputType { A, // input a B, // input b OTHER //other input }; const int NUM_INPUT = 3; if(s == " ") return false; //get rid of space int start = s.find_first_not_of(" "); int end = s.find_last_not_of(" "); string ss = s.substr(start, end-start+1); int n = ss.size(); int state = 0; const int transitionTable[][NUM_INPUT] = { 0, 1, -1, //技巧就是从0状态开始建可能效果更好 -1, 0, -1, }; for(int i = 0; i < n; ++i) { InputType type = OTHER; if(ss[i] == 'a') { type = A; } else if(ss[i]=='b') { type = B; } else{ type = OTHER; } state = transitionTable[state][type]; if(state == -1) return false; } return state == 0 ;}bool isABBA(string s){ enum InputType { A, // input a B, // input b OTHER //other input }; const int NUM_INPUT = 3; if(s == " ") return false; //get rid of space int start = s.find_first_not_of(" "); int end = s.find_last_not_of(" "); string ss = s.substr(start, end-start+1); int n = ss.size(); int state = 1; const int transitionTable[][NUM_INPUT] = { -1, -1, -1, //error state 1 , 2 , -1, 3 , 2 , -1, 3 , -1, -1 }; for(int i = 0; i < n; ++i) { InputType type = OTHER; if(ss[i] == 'a') { type = A; } else if(ss[i]=='b') { type = B; } else{ type = OTHER; } state = transitionTable[state][type]; if(state == -1) return false; } return state == 3 ||state==2;}bool isIpv4Add(string s){ enum InputType { NUM, // input umber DIGIT, // input igit OTHER //other input }; const int NUM_INPUT = 3; if(s == " ") return false; //get rid of space int start = s.find_first_not_of(" "); int end = s.find_last_not_of(" "); string ss = s.substr(start, end-start+1); int n = ss.size(); int state = 1; const int transitionTable[][NUM_INPUT] = { // d . other/*0*/ 0, 0, 0,//Error state/*1*/ 2, 0, 0, 3, 5, 0, 4, 5, 0, 0, 5, 0,/*5*/ 6, 0, 0, 7, 9, 0, 8, 9, 0, 0, 9, 0,/*9*/ 10,0, 0, 11,13,0, 12,13,0, 0, 13,0,/*13*/ 14,0, 0, 15,0, 0,//final state 16,0, 0,//final state 0, 0, 0 //final state }; for(int i = 0; i < n; ++i) { InputType type = OTHER; if((ss[i]-'0')>=0 && (ss[i]-'0')<=9) { type = NUM; } else if(ss[i]=='.') { type = DIGIT; } else{ type = OTHER; } state = transitionTable[state][type]; if(state == 0) return false; } return state == 14 || state==15 || state==16 ;}int main(){ string s; for(int i=0;i<10;i++){ cin>>s; cout << "Hello world!" << endl; if(isABBA(s)){ cout<<"yes"<

转载地址:http://xtyjn.baihongyu.com/

你可能感兴趣的文章
博客中能用到的代码(二)
查看>>
博客中能用到的代码
查看>>
打字机效果-type-js
查看>>
解决github头像加载不出来
查看>>
在linux上安装python3-7
查看>>
Hexo博客美化之添加公告板---基于Ayer主题有两种模式
查看>>
基于MFC和二维变换的画图软件
查看>>
基于WINDLX的系统结构实验
查看>>
Hexo竟然可以展示PDF
查看>>
Hexo插件推荐
查看>>
Hexo博客搭建超级指南
查看>>
博客前端禁止鼠标右键,F12,F5
查看>>
《微机原理与接口技术》知识点总结
查看>>
《编译原理》知识点考点超全总结
查看>>
《计算机网络》知识点超详细总结
查看>>
《数据库》基础知识点考点大全
查看>>
《算法分析与设计》笔记总结
查看>>
《软件工程》知识点超详细总结归纳
查看>>
《计算机系统结构》详细知识点总结
查看>>
《深度学习》“花书”知识点笔记
查看>>