洛谷 P3952 时间复杂度
#include <iostream>
#include <cstdio>
#include <stack>
#include <set>
#include <string>
using namespace std;
int getNumber(string s) {
int res = 0;
for(int i = 0; i < s.length(); i++) {
res *= 10;
res += s[i] - '0';
}
return res;
}
int getO(string s) {
int res = 0;
if(s[2] == '1') return 0;
for(int i = 4; s[i] != ')'; i++) {
res *= 10;
res += s[i] - '0';
}
return res;
}
int main() {
int t; cin >> t;
stack<int> cache;
for(int i = 0; i < t; i++) {
int l; string o;
scanf("%d", &l); cin >> o;
stack<string>name;
set<string>check_name;
int finallyO = 0, tempO = 0;
bool flag = true, alone = false;
int EFs = 0;
for(int j = 0; j < l; j++) {
char ch; getchar();
scanf("%c", &ch);
if(ch == 'E') {
EFs -= 1; tempO -= 1;
if(!name.empty()) {
check_name.erase(name.top()); name.pop();
}
if(!EFs) {
alone = false;
tempO = 0;
}
} else if(ch == 'F') {
EFs += 1;
string v, x, y; cin >> v >> x >> y;
if(check_name.find(v) != check_name.end()) flag = false;
else {
check_name.insert(v);
name.push(v);
if(x[0] >= '0' && x[0] <= '9' && y[0] == 'n') {
if(!alone) tempO++;
finallyO = max(finallyO, tempO);
} else if(x[0] == 'n' && y[0] >= '0' && y[0] <= '9'
|| getNumber(x) > getNumber(y))
alone = true;
}
}
}
if(EFs || !flag) puts("ERR");
else if(getO(o) == finallyO) puts("Yes");
else puts("No");
}
return 0;
}