# UVa 271 Simply Sintax ###### tags: `Algorithm` `Ans` ```cpp= #include<iostream> using namespace std; #define maxN 10000000 int rear; int stk[maxN]; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; while (getline(cin,s)) { rear=0; int len=s.size(); for( int i=0; i<len; i++) { if (s[i]=='N') { stk[rear++] = -1; } else if (s[i]=='C' || s[i]=='D' || s[i]=='E' || s[i]=='I') { stk[rear++] = -2; } else if ('p'<=s[i]&&s[i]<='z') { stk[rear++] = 1; } if (rear<2) continue; while (stk[rear-1]>0&&stk[rear-2]<0) { if (stk[rear-2]==-1) { stk[(--rear)-1] = 1; } else if (stk[rear-2]==-2) { stk[(--rear)-1] = -1; } } } if (rear==1&&stk[0]==1) cout << "YES\n"; else cout << "NO\n"; } } ``` --- 利用for迴圈從後方檢查回來 如果是`p~z`就將句子數量+1 如果是`N`,只要N後面有句子就能連接成一句 如果是`C`,`D`,`E`,`I`,只要後面有2個或2個以上的句子就連接成一句 ```cpp= //by Koios1143 #include<iostream> #include<memory.h> using namespace std; int main(){ //--------------------------------- #ifdef local freopen("UVa271.txt","r",stdin); freopen(".txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //--------------------------------- bool alpha[128]; memset(alpha,false,sizeof(alpha)); for(int i='p' ; i<='z' ; i++){ alpha[i]=true; } alpha['N']=alpha['C']=alpha['D']=alpha['E']=alpha['I']=true; string s; while(cin>>s){ int size=s.size(); bool YN=true; int cnt=0; for(int i=size-1 ; i>=0 ; i--){ if(!alpha[s[i]]){ YN=false; break; } if(s[i]=='N'){ if(cnt>=1){ //cnt--; } else{ YN=false; break; } } else if(s[i]>='A'&&s[i]<='Z'){ if(cnt>=2) cnt--; else{ YN=false; break; } } else{ cnt++; } } if(cnt>1) YN=false; if(YN) cout<<"YES\n"; else cout<<"NO\n"; } return 0 ; } ``` 附註:其實題目有說輸入保證只有那幾個字母,所以其實不用用bool判斷
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up