$NOIP2017$時間複雜度

\(NOIP2017\)時間複雜度c++

大模擬,可是主要看思路,思路簡單了會很好寫。數組

考慮到有無效循環這種東西,咱們開個\(int:\text{For}=0,\text{Vor}=0,\text{IN}=1\)來記錄,遇到\(F\)\(++\),遇到\(E\)\(--\)spa

開個\(\text{Vor}\)記個當前有效循環次數。若是遇到沒法進入的循環,關掉\(\text{IN}\),記錄一下\(\text{Pos}=\text{For}\)(由於最多隻會有一個沒法進入的循環起做用)code

對於循環記個變量名(用數組)。get

#define _GLIBCXX_USE_CXX11_ABI 0
#include<bits/stdc++.h>
using namespace std;
const int N=200;
char x;
int Var[30],V[N],Jud[N];
inline void Clear(){do x=getchar();while(x!='\n'&&x!='\r');}
int main(){
#ifndef ONLINE_JUDGE
    freopen("A.in","r",stdin);
#endif
    int T;scanf("%d",&T);
    while(T--)
    {
        int Typ=0,L=0,ERR=0,For=0;string O="";
        int Vor=0,IN=1,Pos=0,Ans=0;memset(Var,0,sizeof(Var));
        memset(Jud,0,sizeof(Jud)); memset(V,0,sizeof(V));
        x=getchar();while(x=='\n'||x=='\r') x=getchar();
        while(x>='0'&&x<='9') L=(L<<3)+(L<<1)+x-'0',x=getchar();
        x=getchar();while(x!='\n'&&x!='\r') O+=x,x=getchar();
        for(int i=0;i<(int)O.size();i++)
            if('0'<=O[i]&&O[i]<='9') Typ=(Typ<<3)+(Typ<<1)+O[i]-'0';
        if(O=="O(1)") Typ=0;
        while(L--)
        {
            if(ERR) {Clear(); continue;}
            x=getchar();while(x=='\n'||x=='\r') x=getchar();
            if(x=='F')
            {
                x=getchar(),x=getchar();For++;
                if(Var[x-'a']) {ERR=1,Clear(); continue;}
                Var[x-'a']=1,V[For]=x-'a';string A="",B="";
                x=getchar(),x=getchar();while(x!=' ') A+=x,x=getchar();
                x=getchar();while(x!='\n'&&x!='\r') B+=x,x=getchar();
                if(A[0]!='n'&&B[0]=='n'&&IN) Vor++,Jud[For]=1;
                else if((A.size()>B.size()||((A.size()==B.size()&&A>B))||(A[0]=='n'&&B[0]!='n'))&&IN) IN=0,Pos=For;
            }
            else
            {
                Ans=max(Ans,Vor);Var[V[For]]=0;
                if(Jud[For]==1) Vor--,Jud[For]=0;For--;
                if(Pos&&For<Pos) IN=1,Pos=0;
            }
            if(For<0) {ERR=1,Clear();continue;}
        }
        if(For) puts("ERR");
        if(!For&&Ans==Typ) puts("Yes");
        if(!For&&Ans!=Typ) puts("No");
    }
}
相關文章
相關標籤/搜索