Advertisement
esraa_syam

Untitled

Dec 6th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. #define nl "\n"
  5. #define ll long long
  6. #define mod  1'000'000'007
  7. #define all(v) v.begin(), v.end()
  8. #define rall(v) v.rbegin(), v.rend()
  9. #define sz(v) (int) v.size()
  10.  
  11. template<typename T = int>
  12. istream &operator>>(istream &in, vector<T> &v) {
  13.     for (auto &x: v) in >> x;
  14.     return in;
  15. }
  16.  
  17. template<typename T = int>
  18. ostream &operator<<(ostream &out, const vector<T> &v) {
  19.     for (const T &x: v) out << x << " ";
  20.     return out;
  21. }
  22.  
  23. void Sira() {
  24.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  25. #ifndef ONLINE_JUDGE
  26.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  27. #endif
  28. }
  29.  
  30. void check(string s) {
  31.     int cntOp = 0, cntDig = 0 , cntZero = 0 , cntDiv = 0;
  32.     bool neg = false;
  33.  
  34.     for (int i = 0; i < sz(s); i++) {
  35.         if (s[i] == '*' || s[i] == '/' || s[i] == '+' || s[i] == '-') {
  36.             cntOp++;
  37.             if (s[i] == '/') cntDiv++;
  38.             if (s[i] == '-') neg = true;
  39.         } else {
  40.             cntDig++;
  41.             if (s[i] == '0') cntZero++;
  42.         }
  43.     }
  44.  
  45.     if (cntDig - cntOp == 1) {
  46.         int cntPos = cntDig - cntZero;
  47.  
  48.         if (cntPos >= cntDiv) cout << "Valid" << nl;
  49.         else cout << "Invalid" << nl;
  50.  
  51.     } else if (cntDig == cntOp) {
  52.         int cntPos = cntDig - cntZero;
  53.  
  54.         if (neg) cntPos--;
  55.  
  56.         if (cntPos >= cntDiv and neg) cout << "Valid" << nl;
  57.         else cout << "Invalid" << nl;
  58.  
  59.     }else {
  60.         cout << "Invalid" << nl;
  61.     }
  62. }
  63.  
  64. void solve() {
  65.     string s;
  66.     cin >> s;
  67.  
  68.     check(s);
  69. }
  70.  
  71. int main() {
  72.     Sira();
  73.     int t = 1;
  74.     cin >> t;
  75.     while (t--) {
  76.         solve();
  77.     }
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement