I feel like my logic is like shit. I wrote such a simple topic for 2 hours, and I still have to write an outline on paper in the future. Otherwise, if you go directly to the computer, you will easily get lost when you encounter multiple bugs. Self, to repair bugs and ignore the logical correctness of the whole piece of code.
When writing this question, I defined while in while, which is simply mentally retarded. It is really funny to think that while itself is a loop, and it is really funny that it does not need to be used as an if judgment statement.
#include <bits/stdc++.h> using namespace std; int candis( char x, char y) // can cancel { if (x == ' ( ' ){ if (y == ' ) ' ) return 1 ; else return 0 ; } if(x == '['){ if(y == ']') return 1; else return 0; } return 0; } int canadd( char x, char y) // can add { if (x == ' [ ' ){ if (y == ' ( ' ||y == ' [ ' ) return 1 ; else return 0 ; } if(x == '('){ if(y == '['||y == '(') return 1; else return 0; } return 0; } intmain () { int t; cin >> t; while(t--){ string s1; int flag = 1; int i=0; cin >> s1; stack<char>s; while(i < s1.length()){ if(s.empty()&&(s1[i] == ')'||s1[i] == ']')){ //情况1 flag = 0; break; } if(s.empty()){ s.push(s1[i]); } else { if (candis(s.top(),s1[i])) { // If it can be cancelled, the stack will pop a s.pop(); } else if (canadd(s.top(),s1[i])){ // If it can be added, push a s.push(s1[i]); } else { // If it is illegal, break flag = 0 directly ; break ; } } i++; } if (!s.empty()) flag = 0 ; // If there is still left in the stack after the end, then no if(flag == 0) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
——