题目
思路(meiyou )
考场30分暴力
正解:
- 思路为先全部读入,再一起处理
- Cal_comp函数:计算时间复杂度,返回值为x的幂数,为-1则程序错误
- Get_comp函数:读入所给的时间复杂度
- Get_num函数:读入x,y的值,为n则为无穷大
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
#define N 105
int t,l,ci_fang;
string read_comp,code[N];
int Get_num(int &x,string s){ // 因为要连续读入x,y两个值,而第二个y的值得起始位置不好判断,所以&x
int len = s.size(),ret = 0;
while(s[x]<'0' || s[x]>'9' && x<len){
if(s[x] == 'n'){
x++;
return 100000;
}
x++;
}
while(s[x]>='0' && s[x]<='9'){
ret *= 10;
ret += (s[x]-'0');
x++;
}
return ret;
}
int Get_comp(){
int x = 3;
if(read_comp[2] == 'n') return Get_num(x,read_comp);
return 0;
}
bool used[30],pos[30];
int Cal_comp(){
memset(used,0,sizeof(used));
memset(pos,0,sizeof(pos));
int ret = 0,now = 0,x;
int flag = -1;
stack <int> s;
for(int i=1; i<=l; i++){
if(code[i][0] == 'F'){
int bl = code[i][2] - 'a';; // save bian liang
if(used[bl]) return -1;
s.push(bl);
used[bl] = true;
x = 4;
int a = Get_num(x,code[i]);
int b = Get_num(x,code[i]);
if(b-a > 1000 && flag == -1){ // n
now++;
ret = max(ret,now);
pos[bl] = true;
}else if(a>b && flag == -1){
flag = bl;
}
}else if(code[i][0] == 'E'){
if(s.empty()) return -1;
int bl = s.top();
s.pop();used[bl] = false;
if(flag == bl) flag = -1;
if(pos[bl]){
pos[bl] = false;
now--;
}
}
}
if(s.size()) return -1;
return ret;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d ",&l);
getline(cin,read_comp);
int target_comp = Get_comp(); // read_comp : O(n^read_comp)
for(int i=1; i<=l; i++)
getline(cin,code[i]);
int compare_comp = Cal_comp();
if(compare_comp == -1) puts("ERR");
else{
if(compare_comp == target_comp)
puts("Yes");
else
puts("No");
}
}
return 0;
}