nq1s788

стресс тесты скобки

Jun 7th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector")
  2. // #pragma GCC target("avx")
  3.  
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <map>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <unordered_set>
  10. #include <set>
  11. #include <queue>
  12.  
  13. #include <cstdio>
  14. #include <cmath>
  15.  
  16. using namespace std;
  17.  
  18. vector<string> psp;
  19.  
  20. void gen(string s, int open, int close) {
  21.     if (open + close == 8) {
  22.         psp.push_back(s);
  23.         return;
  24.     }
  25.     if (open < 4) gen(s + '(', open + 1, close);
  26.     if (open > close) gen(s + ')', open, close + 1);
  27. }
  28.  
  29. string andr(string a, string b) {
  30.     int n = 8;
  31.     int bl1=0;
  32.     int bl2=0;
  33.     int p1=0;
  34.     int p2=0;
  35.     int k=0;
  36.     for (int i = 0; i < n; i++) {
  37.         if (bl1 < 0 || bl2 <0) {
  38.             ///cout << "NO" << endl;
  39.             k=1;
  40.             return "NO";
  41.         }
  42.         if (a[i] == '(') {
  43.             bl1++;
  44.             p1++;
  45.         }
  46.         else {
  47.             bl1--;
  48.         }
  49.  
  50.         if (b[i] == '(') {
  51.             bl2++;
  52.             p2++;
  53.         }
  54.         else {
  55.             bl2--;
  56.         }
  57.         if ( (p1 < n/2 && p2 < n/2) || (p1 >= n/2 && p2 >= n/2) ) {
  58.             if (a[i]!=b[i]) {
  59.                 if (bl1 > bl2 && b[i]==')' ) {
  60.                     bl1-=2;
  61.                     bl2+=2;
  62.                     p2++;
  63.                 }
  64.                 else if (bl1 < bl2 && a[i]==')' ) {
  65.                     bl2-=2;
  66.                     bl1+=2;
  67.                     p1++;
  68.                 }
  69.             }
  70.         }
  71.         else if (p1 < n/2 && p2>=n/2) {
  72.             if (a[i] != b[i] && a[i] == ')') {
  73.                 p1++;
  74.                 p2--;
  75.                 bl1+=2;
  76.                 bl2-=2;
  77.             }
  78.         }
  79.         else if (p1 >= n/2 && p2 < n/2) {
  80.             if (a[i] != b[i] && b[i] == ')') {
  81.                 p2++;
  82.                 p1--;
  83.                 bl2+=2;
  84.                 bl1-=2;
  85.             }
  86.         }
  87.     }
  88.     if (bl1==0 && bl2==0) {
  89.         ///cout <<"YES" << endl;
  90.         return "YES";
  91.     }
  92.     else if (k==0) {
  93.         ///cout << "NO" << endl;
  94.         return "NO";
  95.     }
  96. }
  97.  
  98. string avt(string s, string t) {
  99.     int n = 8;
  100.     string r1 = "", r2 = "";
  101.     bool bj = 1;
  102.     for (int i = 0; i < n; i++) {
  103.         if (s[i] == t[i]) r1 += s[i], r2 += t[i];
  104.         else {
  105.             if (bj) r1 += '(', r2 += ')';
  106.             else r1 += ')', r2 += '(';
  107.             bj ^= 1;
  108.         }
  109.     }
  110.     int t1 = 0, t2 = 0;
  111.     bj = 1;
  112.     for (int i = 0; i < n; i++) {
  113.         if (r1[i] == '(') ++t1; else --t1;
  114.         if (r2[i] == '(') ++t2; else --t2;
  115.         if (t1 < 0 || t2 < 0) bj = 0;
  116.     }
  117.     if (t1 != 0 || t2 != 0) bj = 0;
  118.     if (bj) return "YES";
  119.     else return "NO";
  120.  
  121. }
  122.  
  123.  
  124. int main() {
  125.     gen("", 0, 0);
  126.     for (auto a : psp) {
  127.         for (auto b : psp) {
  128.             vector<int> diff;
  129.             for (int i = 0; i < 8; i++) {
  130.                 if (a[i] != b[i]) diff.push_back(i);
  131.             }
  132.             for (int x = 1; x < (1 << (int)diff.size()); x++) {
  133.                 set<int> fdiff;
  134.                 for (int i = 0; i < (int)diff.size(); i++) {
  135.                     if (x & (1 << i)) fdiff.insert(diff[i]);
  136.                 }
  137.                 string a1, b1;
  138.                 for (int i = 0; i < 8; i++) {
  139.                     if (fdiff.count(i)) {
  140.                         a1 += b[i];
  141.                         b1 += a[i];
  142.                     } else {
  143.                         a1 += a[i];
  144.                         b1 += b[i];
  145.                     }
  146.                 }
  147.  
  148.                 string AVT = avt(a1, b1);
  149.                 string ANDR = andr(a1, b1);
  150.                 if (ANDR[0] != AVT[0]) {
  151.                     cout << AVT << ' ' << ANDR << endl;
  152.                     cout << a1 << " " << b1 << endl;
  153.                     cout << "------------\n";
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     return 0;
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment