Advertisement
SkeptaProgrammer

Untitled

Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. bool isAllSimilar(string s, int l)
  8. {
  9.     bool result = true;
  10.     for (int i = 0; i < l&&result; i++)
  11.     {
  12.         if (s[0] != s[l - i])
  13.             result = false;
  14.     }
  15.     return result;
  16. }
  17. string swapPalindrom(string s , int l)
  18. {
  19.     bool isswap = false;
  20.     for (int i = 0; i < l && !isswap; i++)
  21.     {
  22.         if (s[0] != s[l - i - 1])
  23.         {
  24.             swap(s[0], s[l - i - 1]);
  25.             isswap = true;
  26.             return s;
  27.         }
  28.     }
  29.     if (!isswap)
  30.     {
  31.         return "-1";
  32.     }
  33.     return s;
  34. }
  35.  
  36. int main()
  37. {
  38.     int t, l = 0; bool b = true;
  39.     string s;
  40.     cin >> t;
  41.     for (int i = 0; i < t; i++)
  42.     {
  43.         cin >> s;
  44.         l = s.length();
  45.         if (l == 1)
  46.         {
  47.             cout << -1 << endl;
  48.             continue;
  49.         }
  50.         if (isAllSimilar(s, l))
  51.         {
  52.             cout << -1 << endl;
  53.             continue;
  54.         }
  55.         for (int j = 0; i < l/2 && b; ++j)
  56.         {
  57.             if (s[j] != s[l - j - 1])
  58.             {
  59.                 b = true;
  60.             }
  61.             else b = false;
  62.         }
  63.         if (!b)
  64.         {
  65.             s = swapPalindrom(s, l);
  66.             if (s != "-1")
  67.                 cout << s << endl;
  68.             else
  69.                 cout << -1 << endl;
  70.         }
  71.         else cout << s<<endl;
  72.         b = true;
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement