PAT_A1051

第一次未AC代码:

#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;

string str;
stack<int> s;

int main(void)
{
    freopen("in.txt","r",stdin);
    
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);  //input 
    char c = getchar();
    
    for(int j = 1; j <= k; j++){
        while(!s.empty())    s.pop();     //初始化 
        
        getline(cin, str);               //输入 
        
        for(string::iterator it = str.begin(); it < str.end(); it++){
            if(*it == ' ')    str.erase(it);     //消掉空格 
        }

        //处理第一个数 
        int count = 0,tmp;            
        if((str[0] - '0') > n){
            printf("NO
");
            continue;
        }
        else{
            for(int i = 1; i <= (str[0] - '0'); i++)
                s.push(i);
            tmp = s.top();
            s.pop();
            count++;
        }
        
        while(count < str.size()){
            if(s.empty()){
                for(int i = tmp + 1; i <= (str[count] - '0'); i++)
                    s.push(i);
                if(s.size() > n)    
                    break;
                else{
                    tmp = s.top();
                    s.pop();
                    count++; 
                }
            }
            else{
                if((str[count] - '0') == s.top()){
                    s.pop();
                    count++;
                }
                else if((str[count] - '0') > tmp){
                    for(int i = tmp + 1; i <= (str[count] - '0'); i++)
                        s.push(i);
                    if(s.size() > n)    
                        break;
                    else{
                        tmp = s.top();
                        s.pop();
                        count++;
                    }    
                }
                else    break;
            }
        }    
        
        if(s.empty())
            printf("YES
");
        else
            printf("NO
");
        
    } 
    
    fclose(stdin);
    return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;

const int maxn = 1010;
int a[maxn];
stack<int> s;

int main(void)
{
    freopen("in.txt","r",stdin);
    
    int m,n,k;
    scanf("%d%d%d",&m,&n,&k);
    while(k--){
        bool sign;
        int tmp = 1;
        while(!s.empty()){     //初始化栈 
            s.pop();
        }
        
        for(int i = 1; i <= n; i++){     //输入弹出序列 
            scanf("%d",&a[i]); 
        }
        
        for(int i = 1; i <= n; i++){
            s.push(i);
            
            if(s.size() > m){     //判断是否符合长度 
                sign = false;
                break;
            }
            
            while(!s.empty() && s.top() == a[tmp]){
                tmp++;
                s.pop();
            }
            
        }
        
        if(s.empty())
            sign = true;
        else 
            sign = false;
            
        if(sign)    
            printf("YES
");
        else
            printf("NO
");
    }
    
    fclose(stdin);
    return 0;
}
原文地址:https://www.cnblogs.com/phaLQ/p/10461189.html