Advertisement
newb_ie

bracket sequence checker

Nov 7th, 2021
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. int main () {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8. cout.tie(nullptr);
  9. string s = "()()(())((()))"; // open = 1
  10. int open = 0;
  11. int n = (int) s.size();
  12. //~ bool f = true;
  13. for (int i = 0; i < n; ++i) {
  14. if (s[i] == '(') {
  15. ++open;
  16. } else {
  17. --open;
  18. }
  19. if (open < 0) {
  20. //close bracket beshi ache
  21. cout << "INVALID\n";
  22. return 0;
  23. }
  24. }
  25. //ekhane asar mane hocche open < 0 execute hoy ni
  26. if (open > 0) {
  27. //open bracket beshi ache
  28. cout << "INVALID\n";
  29. } else {
  30. cout << "VALID\n";
  31. }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement