再寫一道大模擬題。
由於是限時寫的,相當於考場代碼,亂的一批。
題目鏈接:P3952 時間複雜度
先記幾個教訓:
- 字符串形式的數字比較大小老老實實寫函數,字典序都搞錯幾次了
- 棧空的時候不但pop()會RE,top()訪問棧頂也會RE
- 數字以字符形式讀入要考慮位數超過一位
思路:
1.我用的是在線做法。
2.使用結構體存儲一個循環體,包括變量名、起始與結束、是否與n有關、是否能夠進入(這一點麻煩了,在棧裏推入是否與n有關以及是否能夠進入就行了)
3.使用一個變量存儲當前進入的層數(只記含n的),當壓入一個“與n有關”時++,當彈出一個“與n有關”時--,取最大值
4.使用一個bool存儲當前循環是否能進入。當彈出一個不能進入的結構體時取消“不能進入”的狀態(如果已在“不能進入”的狀態,結構體不會被記作“不能進入”)
源碼:
//MiserWeyte is now "mzWyt"
#include <bits/stdc++.h>
using namespace std;
int n, tgt, rel, curr = 0, maxx;
bool used[30];
string tar;
bool err, notin;
struct sts{
char tpe, nam;
string sta, end;
bool use, nin;
};
bool le(string a, string b){
if(a=="n" || b=="n" ) return false;
int numa = atoi(a.c_str());
int numb = atoi(b.c_str());
return numa > numb;
}
stack <sts> s;
void init(){
err = false;
memset(used, 0, sizeof(used));
curr = 0;
maxx = 0;
notin = false;
while(s.size()) s.pop();
}
int main(){
int t;
cin >> t;
while(t--){
init();
cin >> n >> tar;
tgt = 0;
if(tar[2] != '1'){
for(int i=0; i<tar.length(); i++){
if(tar[i] >= '0' && tar[i] <= '9'){
tgt *= 10;
tgt += tar[i] - '0';
}
}
}
for(int i=0; i<n; i++){
sts temp;
cin >> temp.tpe;
if(temp.tpe == 'F'){
cin >> temp.nam ;
cin>> temp.sta;
cin>> temp.end;
temp.use = false;
temp.nin = false;
if(used[temp.nam - 'a']){
err = true;
// break;
}
used[temp.nam - 'a'] = true;
if(notin){
s.push(temp);
}
else if(temp.sta == "n" && temp.end == "n") s.push(temp);
else if(temp.sta != "n" && temp.end == "n"){
temp.use = true;
curr ++;
maxx = max(maxx, curr);
s.push(temp);
}
else if((temp.sta == "n" && temp.end != "n") || le(temp.sta, temp.end)){
notin = true;
temp.nin = true;
s.push(temp);
}
else s.push(temp);
}
else{
if(s.empty()){
err = true;
continue;
}
if(s.size() && s.top().use && !s.top().nin) curr --;
// cout << s.top().use;
if(s.size() && s.top().nin) notin = false;
used[s.top().nam - 'a'] = false;
if(s.size()) s.pop();
}
// cout << "curr" << curr << endl;
// if(notin) cout << "NOTIN" << endl;
}
if(s.size()) err = true;
if(err) cout << "ERR\n";
else{
if(maxx == tgt) cout << "Yes\n";
else cout << "No\n";
// cout << curr << endl;
}
}
}