SHARE
TWEET

palindromic cut

a guest Feb 17th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. #define MAX 400005
  3. using namespace std;
  4. int n;
  5. char str[MAX];
  6. int cnt[130]={0};
  7. vector<char> odd,even;
  8. int main()
  9. {
  10.     scanf("%d",&n);
  11.     scanf("%s",&str);
  12.     for(int i=0;i<n;i++) cnt[str[i]]++;
  13.     for(int i='0';i<='z';i++)
  14.     {
  15.         if(cnt[i]<=0) continue;
  16.         if(cnt[i]%2)
  17.         {
  18.             odd.push_back((char)i);
  19.             cnt[i]--;
  20.         }
  21.         while(cnt[i])
  22.         {
  23.             even.push_back(i);
  24.             cnt[i]-=2;
  25.         }
  26.     }
  27.  
  28.     char ans[MAX];
  29.     if(odd.empty())
  30.     {
  31.         printf("1\n");
  32.         for(int i=0;i<n/2;i++) ans[i]=ans[n-1-i]=even[i];
  33.         ans[n]='\0';
  34.         printf("%s\n",ans);
  35.         return 0;
  36.     }
  37.  
  38.     while(even.size() % odd.size())
  39.     {
  40.         odd.push_back(even.back());
  41.         odd.push_back(even.back());
  42.         even.pop_back();
  43.     }
  44.  
  45.     printf("%d\n",odd.size());
  46.     int len=n/odd.size();
  47.     while(!odd.empty())
  48.     {
  49.         ans[len/2]=odd.back();
  50.         odd.pop_back();
  51.         for(int j=0;j<len/2;j++)
  52.         {
  53.             ans[j]=ans[len-1-j]=even.back();
  54.             even.pop_back();
  55.         }
  56.         ans[len]='\0';
  57.         printf("%s ",ans);
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top