jJHZ @ 2025-01-04 21:18:12
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define TLE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
using namespace std;
stack<int> s;
map<char, int> mp;
int main()
{
int T;
TLE;
cin >> T;
while(T--){
int n;
int m = 0;
cin >> n;
string standard;
cin >> standard;
for (int i = 1; i <= n;i++){
char kind, var, start, end;
cin >> kind;
int size = s.size();
m = max(m, size);
if(kind=='F'){
cin >> var >> start >> end;
if(mp[var]==1){
cout << "ERR" << '\n';
break;
}
else{
mp[var]=1;
s.push(var);
}
if(start=='n'){
break;
}
if(end=='n'){
m++;
}
if(start>end){
break;
}
}else{
if(s.empty()){
cout << "ERR"<<'\n';
break;
}
mp[s.top()] = 0;
}
}
if(!s.empty()){
cout << "ERR" << '\n';
break;
}
if(m==0){
if(standard=="O(1)"){
cout << "Yes" << '\n';
}
else{
cout << "No" << '\n';
}
}
else{
string ans = "O(n^m)";
ans[4] = (m + '0');
if(ans==standard){
cout << "Yes" << '\n';
}
else{
cout << "No" << '\n';
}
}
}
return 0;
}
by ZJ_lzz @ 2025-01-04 21:49:12
循环的时间复杂度取决于最内层的计算次数,即嵌套最深的一层循环的计算次数。
循环的嵌套和括号序列的嵌套类似,所以我们可以借助栈来遍历整个代码序列。
当遇到FOR语句时,将该循环压入栈顶,当遇到END语句时,将栈顶的循环弹出。那么栈中从底到顶的序列就是当前循环从外到内嵌套的序列。
对于每层循环FOR
by jJHZ @ 2025-01-04 21:51:19
@ZJ_lzz
谢谢