Advertisement
Infernale

Longest Peek Palindrome [NCTU Floor 20]

Dec 25th, 2019
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. void processString(string &input){
  8.     string result;
  9.     remove_copy_if(input.begin(), input.end(), back_inserter(result), ptr_fun<int, int>(&ispunct));
  10.     for_each(result.begin(), result.end(), [](char &c){
  11.         c = ::toupper(c);
  12.     });
  13.     input = result;
  14. }
  15.  
  16. void solve(string input){
  17.     processString(input);
  18.     int maxLength = 1;
  19.     int low, high, start = 0;
  20.     int len = input.length();
  21.     for(int i = 1; i < len; ++i){
  22.         low = i - 1;
  23.         high = i + 1;
  24.         while(input[low] == input[high] && low >= 0 && high < len){
  25.             if(high - low + 1 > maxLength){
  26.                 start = low;
  27.                 maxLength = high - low + 1;
  28.             }
  29.             --low;
  30.             ++high;
  31.         }
  32.         low = i - 1;
  33.         high = i;
  34.         while(input[low] == input[high] && low >= 0 && high < len){
  35.             if(high - low + 1 > maxLength){
  36.                 start = low;
  37.                 maxLength = high - low + 1;
  38.             }
  39.             --low;
  40.             ++high;
  41.         }
  42.     }
  43.     cout << input.substr(start, maxLength) << endl;
  44. }
  45.  
  46. int main(){
  47.     string in;
  48.     while(cin >> in){
  49.         solve(in);
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement