跳到正文

题解

题解:UVA271 Simply Syntax

DUCK_inanimated_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
UVA271

版本

  1. v1当前

题解:UVA271 Simply Syntax

思路

很顺的一道模拟,细节要注意一下。

  • 由于要求中特殊字符在前面,所以从后往前搜。

  • 进行完条件 3 时注意要将 now 的数量减一,因为此时只能算作一个合法的字串。

  • 注意条件 0 不是没用的。

代码

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
signed main(){
    string s;
    while(cin>>s){
        reverse(s.begin(),s.end());
        int n=s.size();s=" "+s;//cout<<s<<" ";
        int now=0,flag=0;
        for(int i=1;i<=n;i++){
            if(s[i]=='N'){
                if(now>=1)continue;
                else {
                    flag=1;break;
                }
                //cout<<"aqdq";
            }else if(s[i]=='C'||s[i]=='D'||s[i]=='E'||s[i]=='I'){
                if(now>=2)now--;
                else {
                    flag=1;break;
                }
                //cout<<"aqeq";
            }else if(s[i]>='p'&&s[i]<='z'){
                now++;
                //cout<<"aqqq";
            }else{
                flag=1;break;
            }
        }
        if(flag==1||now!=1)cout<<"NO\n";
        else cout<<"YES\n";
    }
	return 0;
}
原文