tungggg

contest xau ki tu palindrome

Mar 17th, 2022
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define pub push_back
  10. #include<bits/stdc++.h>
  11.  
  12. bool isPalindrome (string s){  
  13.     string t= s;
  14.     reverse ( s.begin(),s.end());
  15.     return t== s;
  16. }
  17.  
  18. int main() {
  19.     string s;
  20.     getline(cin,s);
  21.     stringstream ss(s);
  22.     string t;
  23.     vector<string > v;
  24.     map<string, int > mp;
  25.     int count =0;
  26.     while ( ss>> t) {
  27.         count ++ ;
  28.         if ( isPalindrome (t) ){
  29.             mp[t]=count;
  30.         }
  31.     }
  32.    
  33.     for (auto x:mp){
  34.         v.pub(x.first);
  35.     }
  36.    
  37.     for (int i=0;i<v.size();i++){
  38.         for (int j=i+1;j<v.size();j++){
  39.             if ( v[i].size()> v[j].size()){
  40.                 swap(v[i],v[j]);
  41.             }
  42.             else if ( v[i].size() == v[j].size()){
  43.                 if (mp[v[i]] > mp[v[j]]){
  44.                     swap (v[i],v[j]);
  45.                 }
  46.             }
  47.            
  48.         }
  49.     }
  50.    
  51.    
  52.     for (auto x: v){
  53.         cout<<x<<" ";
  54.     }
  55.     /* Enter your codfore here. Read input from STDIN. Print output to STDOUT */  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment