题目:给了slury的语法(由slimp和slump组成),语法分析判断
注意:函数变量用形参
函数设置一个begin变量真的很方便
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <ctgmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool slump(char m[61],int &begin){
if(m[begin]=='D' || m[begin]=='E'){
if (m[++begin] == 'F') {
while (m[++begin]=='F');
if(m[begin]=='G') return true;
else if(m[begin]=='D' || m[begin]=='E') return slump(m, begin);
else return false;
}
else return false;
}
else return false;
}
//DFG
// 01234567
//DFG, EFG, DFFFFFG, DFDFDFDFG, DFEFFFFFG
//DFEFF, EFAHG, DEFG, DG, EFFFFDG
bool slimp(char m[61],int &begin){
if (m[begin] == 'A') {
begin++;
if (m[begin] == 'H') return true;
else if (m[begin] == 'B'){
if(slimp(m, ++begin)){
if (m[++begin] == 'C') return true;
else return false;
}
else return false;
}
else if (m[begin]=='D' || m[begin]=='E'){
if(slump(m, begin)){
if (m[++begin] == 'C') return true;
else return false;
}
else return false;
}
else return false;
}
else return false;
}
//AH, ABAHC, ABABAHCC, ADFGC, ADFFFFGC, ABAEFGCC, ADFDFGC
int main(){
int num;
cin>>num;
char input[61];
cout<<"SLURPYS OUTPUT"<<endl;
while (num--) {
cin >> input;
int b = 0;
// cout<<slimp(input, b);
if (slimp(input, b)) {
if (slump(input, ++b)) {
if (b==(strlen(input)-1)) {//防止出现AHDFGA
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
else cout<<"NO"<<endl;
}
else cout<<"NO"<<endl;
}
cout<<"END OF OUTPUT"<<endl;
return 0;
}