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

地址:http://codeforces.com/contest/1046/problem/H

因为a-z只有26个,可以用二进制位表示,将每个字符串压缩,如果一个字符串的某个字母出现偶数次则可以去掉这个字母,出现奇数次,则加到二进制数中,就是用一个数代表一个字符串,用map统计每个数出现次数,因为一压缩就会有很多相同的数出现;
如果x = mp[ans]是ans这个数出现的个数(也是字符串的个数)结果加上x*(x - 1) / 2;
在n^2遍历每两个不同的数,异或如果为0或者是2的k次方,则也可以进行配对,x = mp[i],y = mp[j];结果加上x * y;
注:要用long long否则会爆,所有数都要用long long,我就被坑了

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值