1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include<iostream> #include<string> using namespace std;
#define MAXSIZE 100 #define ADDSIZE 10 typedef int Status; typedef char SElemType;
typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack;
Status Initstack(SqStack &s,int e=MAXSIZE) { s.base=new SElemType[e]; s.top=s.base; s.stacksize=e; return 1; }
Status push(SElemType e,SqStack &s) { if(s.top-s.base>=s.stacksize) { /*s.base=(SElemType*)realloc(s.base,(s.stacksize+ADDSIZE)* sizeof(SElemType)); if(!s.base) exit(EOVERFLOW); s.top=s.base+s.stacksize; s.stacksize+=ADDSIZE;*/ return 0; } *s.top++=e; return 1; }
Status pop(SElemType &e,SqStack &s) { if(s.top==s.base)return 0; e=*--s.top; return 1; }
Status GetTop(SElemType &e,SqStack &s) { if(s.top==s.base)return 0; e=*(s.top-1); return 1; }
bool Isright(string s,int e) { int i=0,f=1; SqStack sq; Initstack(sq,e); while (s[i] != '\0') { if(s[i]=='S') { if (push(s[i],sq) == 1) i++; else { f = 0; break; } } else { if (pop(s[i],sq) == 1) i++; else { f = 0; break; } } } if(sq.top==sq.base&&s[i]=='\0') f=1; else f=0; return f == 1; } bool IsRight(string s,int L) { int i=0,flag=1; SqStack sq; Initstack(sq,L); while(s[i]!='\0') { if(s[i]=='S') { if(push(s[i],sq)==1) i++; else { flag=0; break; } } else { if(pop(s[i],sq)==1) i++; else { flag=0; break; } } } if((sq.top==sq.base) && s[i]=='\0') flag=1; else flag=0; return flag == 1; }
int main() { int m,n; string ss; cin>>m>>n; for (int u = 0; u < m; ++u) { cin>>ss; if (IsRight(ss,n)== true) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }
|