Advertisement
SomniP

Поиск в длинном числе - чисел полиндромов максимальной длины

Dec 11th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     SetConsoleCP(1251);
  12.     SetConsoleOutputCP(1251);
  13.     system("color 0A");
  14.  
  15.     cout << "Введите число любой длины" << endl;
  16.     string s;
  17.     getline(cin, s);
  18.     vector<string> box;
  19.     for (size_t left = 0u; left < s.size() - 1u; ++left)
  20.     {
  21.         for (size_t right = left + 1u; right < s.size(); ++right)
  22.         {
  23.             bool flag = true;
  24.             size_t ind = 0u;
  25.             for (size_t u = left; u <= right; ++u)
  26.             {
  27.                 if (s[u] != s[right - ind++])
  28.                 {
  29.                     flag = false;
  30.                     break;
  31.                 }
  32.             }
  33.             if (flag)
  34.             {
  35.                 string temp;
  36.                 for (size_t u = left; u <= right; ++u)
  37.                 {
  38.                     temp.push_back(s[u]);
  39.                 }
  40.                 box.push_back(temp);
  41.             }
  42.         }
  43.     }
  44.     auto predicate = [](string s1, string s2)
  45.     {
  46.         return s1.size() > s2.size();
  47.     };
  48.     sort(box.begin(), box.end(), predicate);
  49.     auto max_len = box[0u].size();
  50.     cout << "Самый длиный(ые) палиндром(ы) в числе" << endl;
  51.     for (const auto &word : box)
  52.     {
  53.         if (word.size() == max_len)
  54.         {
  55.             cout << word << endl;
  56.         }
  57.     }
  58.  
  59.     system("pause");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement