Need Help With Problem Palindrome Pairs

Revision en1, by Boshkash_Hates_CP, 2025-01-01 00:31:11

Hello CF , First thing first , happy new year , May this year bring you health , happiness and successes in all you do !

i have struggled to understand Palindrome Pairs solution "in terms of bitmasks" , as i'm newbie.

i didn't thought about the iterative solution as it will be o(n^2) which will case TLE ,so i was trying to get a better solution and i wasn't able to , so i saw the solution which is something like this:

int main()
{
   FastIO;
   ll n ;cin >>n;
   map<ll,ll> mp;
   ll ans = 0;
   while(n--)
   {
      STR s;cin>>s;
      ll m = 0;
      for(auto x : s)
      {
         ll v = x-'a';
         m ^= (1 << v);
      }
      ans += mp[m];
      for(int x = 0 ; x < 26 ; x++)
      {
         m ^= (1 << x);
         ans += mp[m];
         m ^= (1 << x);
      }
      mp[m]++;
   }
   cout << ans;
}

i don't get it , i mean i know to check if a string is a Palindrome or not we get it's mask and check it's power of 2 or not by XOR'ing the values of each char ....help

Tags bitmask

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Boshkash_Hates_CP 2025-01-01 00:31:11 1131 Initial revision (published)