#include <iostream>
#include <string>
using namespace std;
int flag, depth, flagd, t, l, w1, w2;
string code[110], o;
char stack1[110];
int top1;
int stack2[110];
int top2;
int getw1(string str)
{
if (str[2] == '1')
{
return 0;
}
else
{
return str[4] - '0';
}
}
bool defined(char ch)
{
for (int i = 0; i < top1; i++)
{
if (stack1[i] == ch)
{
return true;
}
}
return false;
}
void push1(char ch)
{
stack1[top1++] = ch;
}
int toint(string str)
{
int res = 0;
for (int i = 0; i < str.size(); i++)
{
res = res * 10 + str[i] - '0';
}
return res;
}
int compare(string str1, string str2)
{
int a, b;
if (str1[0] != 'n')
{
a = toint(str1);
}
if (str2[0] != 'n')
{
b = toint(str2);
}
if (str1[0] != 'n' && str2[0] != 'n')
{
return a <= b ? 0 : -1;
}
else if (str1[0] != 'n' && str2[0] == 'n')
{
return 1;
}
else if (str1[0] == 'n' && str2[0] != 'n')
{
return -1;
}
else if (str1[0] == 'n' && str2[0] == 'n')
{
return 0;
}
}
void push2(string str)
{
int p;
for (int i = 4; i < str.size(); i++)
{
if (str[i] == ' ')
{
p = i;
break;
}
}
string a = str.substr(4, p - 4);
string b = str.substr(p + 1, str.size() - p - 1);
int cmp = compare(a, b);
if (cmp == -1)
{
flag = 0;
flagd = depth;
}
int temp = top2 > 0 ? stack2[top2 - 1] : 0;
stack2[top2] = temp + flag * cmp;
top2++;
}
void pop1()
{
top1--;
}
void pop2()
{
int temp = (top2 > 0 ? stack2[top2 - 1] : 0);
if (temp > w2)
{
w2 = temp;
}
top2--;
}
int main()
{
cin >> t;
while (t--)
{
cin >> l >> o;
getline(cin, code[101]);
w1 = getw1(o), flag = 1, top1 = top2 = depth = 0;
int fe = 0, err = 0;
for (int i = 0; i < l; i++)
{
getline(cin, code[i]);
if (code[i][0] == 'F')
{
fe++;
}
else
{
fe--;
}
if (fe < 0)
{
err = 1;
break;
}
}
if (err || fe != 0)
{
cout << "ERR\n";
continue;
}
w2 = 0;
for (int i = 0; i < l; i++)
{
if (code[i][0] == 'F')
{
depth++;
if (defined(code[i][2]))
{
err = 1;
break;
}
push1(code[i][2]);
push2(code[i]);
}
else
{
depth--;
pop1();
pop2();
}
if (flagd == depth + 1)
{
flag = 1;
}
}
if (err)
{
cout << "ERR\n";
}
else if (w1 == w2)
{
cout << "Yes\n";
}
else if (w1 != w2)
{
cout << "No\n";
}
}
return 0;
}
我的思路:先边读入边判断FE匹配情况,再一行一行分FE讨论进行相应操作