Bubble Cup 11 - Finals [Online Mirror, Div. 2] H. Palindrome Pairs

Address: http://codeforces.com/contest/1046/problem/H

Because a-z has only 26 bits, it can be represented by binary bits, and each string can be compressed. If a certain letter of a string appears even times, the letter can be removed and odd times can appear, then it can be added to the binary number, that is, a number is used to represent a string, and map is used to count the number of times each number appears, because a compression will have many identical numbers;
If x = MP [ans] is the number of ANs (also the number of strings), add x*(x - 1) / 2;
Traverse every two different numbers i n n^2, XOR if it is 0 or k power of 2, you can also pair, x = mp[i],y = mp[j]; add x * y to the result;
Note: long long long will be used otherwise it will explode. Long long will be used for all numbers, and I will be hit

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;

#define fi first
#define se second
#define mp1 make_pair

string s[N];
map<char,int>mp;
map<LL,LL>mk;
pair<LL,LL> a[N];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;++i)
    {
        mp.clear();
        cin >> s[i];
        int len = s[i].size();
        for(int j = 0;j < len;++j)
            mp[s[i][j]]++;
        LL ans = 0;
        for(map<char,int>::iterator it = mp.begin();it != mp.end();++it)
        {
            if((it -> se) & 1){
                ans += (1 << ((it -> fi) - 'a'));
            }
        }
        mk[ans]++;
    }
    LL ans = 0;
    int len = 0;
    for(map<LL,LL>::iterator it = mk.begin();it != mk.end();++it)
    {
        a[len++] = mp1(it -> fi,it -> se);
        LL x = it -> se;
        ans += (x * (x - 1)) / 2LL;
    }
    for(int i = 0;i < len;++i)
    {
        for(int j = i + 1;j < len;++j)
        {
            int x = (a[i].fi ^ a[j].fi);
            if(x == 0 || !(x & (x - 1))){
                ans += (a[i].se * a[j].se);
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Posted by donkru on Thu, 26 Dec 2019 15:00:27 -0800