三目运算,我感觉我逻辑很清晰,可惜优化无从下手,超时了(悲)
#include <bits/stdc++.h>
using namespace std;
int f(const string &S, int &pos, int x)
{
int result=0;
while (pos<S.length() and isdigit(S[pos]))
{
result=result*10+(S[pos]-'0');
pos++;
}
if (pos == S.length() || S[pos] != 'x')
{
return result;
}
bool greater=(S[pos + 1] == '>');
pos += 2;
int con = 0;
while (isdigit(S[pos]))
{
con=con*10+(S[pos]-'0');
pos++;
}
pos++;
int truee = f(S, pos, x);
pos++;
int falsee = f(S, pos, x);
if ((greater and x > con) or (!greater and x < con))
{
return truee;
}
else
{
return falsee;
}
}
int main()
{
int m,q;
cin >>m>>q;
string S;
cin>>S;
while(q--)
{
int x;
cin>>x;
int pos=0;
cout<<f(S,pos,x)<<endl;
}
return 0;
}