不构造树的情况下验证先序遍历

string用法参考链接 与 原题链接

#include<cstdio>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string preorder)
{
    string s=preorder;
    bool flag=true;
    while(s.length()>1){
        int index=s.find(",#,#");
        if(index<0){
            flag=false;
            break;
        }
        int start=index;//或者s[start-1]!=',' 
        while(start>0&&s.at(start-1)!=',') start--;
        s=s.substr(0,start)+s.substr(index+3);
    }
    if(!s.compare("#")&&flag)
        return true;
    return false;
}
int main()
{
    string preorder;
    cin>>preorder;
    if(is_valid(preorder))
        printf("win");
    else
        printf("lose");
}
//输入 9,3,4,#,#,1,#,#,2,#,6,#,# 
原文地址:https://www.cnblogs.com/freinds/p/6367205.html