Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. ifstream in("palindrom.in");
  8. ofstream out("palindrom.out");
  9.  
  10. const int lMax = 200005;
  11.  
  12. char s[lMax];
  13. int n;
  14.  
  15. inline bool test_char(int x) //daca caracterul de pe poz x poate fi mijlocul unui palindrom
  16. {
  17.     for(int i = 1; x + i < n; ++i)
  18.         if(s[x + i] != s[x - i])
  19.             return false;
  20.     return true;
  21. }
  22.  
  23. inline bool test_space(int x) //daca spatiul de pe poz x poate fi mijlocul unui palindrom
  24. {
  25.     for(int i = 1; x + i <= n; ++i)
  26.     {
  27.         if(s[x+i-1] != s[x-i])
  28.         {
  29.             return false;
  30.         }
  31.     }
  32.     return true;
  33. }
  34.  
  35. int main()
  36. {
  37.     in >> s;
  38.     n = strlen(s);
  39.     int rasp = n - 1;
  40.     int rasp_space = n - 1;
  41.     for(int i = n / 2; i < n - 1; ++i)
  42.     {
  43.         if(test_char(i))
  44.         {
  45.             rasp = i;
  46.             break;
  47.         }
  48.     }
  49.     for(int i = n / 2; i < n - 1; ++i)
  50.     {
  51.         if(test_space(i))
  52.         {
  53.             rasp_space = i;
  54.             break;
  55.         }
  56.     }
  57.     out << s;
  58.     if(rasp_space <= rasp)
  59.     {
  60.         for(int i = 1; i <= rasp; ++i)
  61.         {
  62.             out << s[rasp_space - (n - rasp_space) - i];
  63.         }
  64.     }
  65.     else
  66.     {
  67.         for(int i = 0; i <= rasp; ++i)
  68.         {
  69.             out << s[rasp - (n - rasp) - i];
  70.         }
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement