[cpp] палиндром-готово

Viewer

copydownloadembedprintName: палиндром-готово
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int t;
  8.     int counter = 0;
  9.     string s;
  10.     cin >> t;
  11.     for (int i = 0; i < t; i++) {
  12.         counter = 0;
  13.         int a, b;
  14.         cin >> a >> b;
  15.         cin >> s;
  16.         for (int j = 0; j < s.length() / 2; j++) {
  17.             if (s[j] != '?' && s[s.length() - j - 1] == '?') s[s.length() - j - 1] = s[j];
  18.             if (s[j] == '?' && s[s.length() - j - 1] != '?') s[j]= s[s.length() - j - 1];
  19.         }
  20.         int a1 = 0, b1 = 0;
  21.         for (int j = 0; j < a + b; j++) {
  22.             if (s[j] == '0') a1++;
  23.             if (s[j] == '1') b1++;
  24.         }
  25.         a1 = a - a1;
  26.         b1 = b - b1;
  27.         if (a1 < 0 || b1 < 0) {
  28.             cout << -1<<endl;
  29.             counter++;
  30.         }
  31.         for (int j = 0; j < (+ b) / 2; j++) {
  32.             if (s[j] == '?' && a1 >= 2) {
  33.                 s[j] = '0';
  34.                 s[s.length() - j - 1] = '0';
  35.                 a1-=2;
  36.             }
  37.             else if (s[j] == '?' && b1 >= 2) {
  38.                 s[j] = '1';
  39.                 s[s.length() - j - 1] = '1';
  40.                 b1-=2;
  41.             }
  42.         }
  43.         if (s.length() % 2 != 0 && a1 == 1) s[(+ b) / 2] = '0';
  44.         if (s.length() % 2 != 0 && b1 == 1) s[(+ b) / 2] = '1';
  45.  
  46.         for (int i = 0; i < s.length(); i++) {
  47.             if (s[i] == '?' && counter==0) {
  48.                 cout << -1<<endl;
  49.                 counter++;
  50.             }
  51.         }
  52.         string s1 = s;
  53.         reverse(s.begin(), s.end());
  54.         if (!= s1 && counter == 0) {
  55.             cout << -1 << endl;
  56.             counter++;
  57.         }
  58.         if(counter==0) cout << s1<<endl;
  59.     }
  60.     
  61.     return 0;
  62. }

Editor

You can edit this paste and save as new:


File Description
  • палиндром-готово
  • Paste Code
  • 26 Sep-2021
  • 1.7 Kb
You can Share it: