artemgf

Палиндромы

Apr 20th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <set>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. bool cor(string s)
  14. {
  15.     long long j=0, i=s.size()-1;
  16.     bool what=true;
  17.     while (i >= j)
  18.     {
  19.         if (s[i] == s[j])
  20.         {
  21.             i--;
  22.             j++;
  23.         }
  24.         else
  25.         {
  26.             what = false;
  27.             break;
  28.         }
  29.     }
  30.     if (what)
  31.         return true;
  32.     else
  33.         return false;
  34. }
  35.  
  36. int main()
  37. {
  38.     string s, prom;
  39.     string max="";
  40.     long long maxpos=10000000;
  41.  
  42.     cin >> s;
  43.  
  44.     for(int i=0;i<s.size();i++)
  45.         for (int j = i + 1; j <= s.size(); j++)
  46.         {
  47.             prom = s.substr(i, j-i);
  48.             if (cor(prom))
  49.             {
  50.                 if (prom.size() > max.size())
  51.                 {
  52.                     max = prom;
  53.                     maxpos = i;
  54.                 }
  55.                 else
  56.                     if (prom.size() == max.size())
  57.                         if (i < maxpos)
  58.                         {
  59.                             max = prom;
  60.                             maxpos = i;
  61.                         }
  62.             }
  63.         }
  64.  
  65.     cout << max;
  66.  
  67.     _getch();
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment