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

                                   H. Palindrome Pairs

                                                                       time limit per test  :2 seconds

                                                                     memory limit per test:256 megabytes

                                                                        input : standard input

                                                                       

output : standard output

After learning a lot about space exploration, a little girl named Ana wants to change the subject.

Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whether it's a palindrome or not, but soon she grew tired of this problem, so she came up with a more interesting one and she needs your help to solve it:

You are given an array of strings which consist of only small letters of the alphabet. Your task is to find how many palindrome pairs are there in the array. A palindrome pair is a pair of strings such that the following condition holds: at least one permutation of the concatenation of the two strings is a palindrome. In other words, if you have two strings, let's say "aab" and "abcac", and you concatenate them into "aababcac", we have to check if there exists a permutation of this new string such that it is a palindrome (in this case there exists the permutation "aabccbaa").

Two pairs are considered different if the strings are located on different indices. The pair of strings with indices (i,j)(i,j) is considered the same as the pair (j,i)(j,i).

Input

The first line contains a positive integer N (1≤N≤100000), representing the length of the input array.

Eacg of the next N lines contains a string (consisting of lowercase English letters from 'a' to 'z') — an element of the input array.

The total number of characters in the input array will be less than 1000000.

Output

Output one number, representing how many palindrome pairs there are in the array.

Examples

input

Copy

​
3
aa
bb
cd

​

output

Copy

1

input

Copy

6
aab
abcac
dffe
ed
aa
aade

output

Copy

6

Note

The first example:

  1. aa ++ bb →→ abba.

The second example:

  1. aab ++ abcac == aababcac →→ aabccbaa
  2. aab ++ aa == aabaa
  3. abcac ++ aa == abcacaa →→ aacbcaa
  4. dffe ++ ed == dffeed →→ fdeedf
  5. dffe ++ aade == dffeaade →→ adfaafde
  6. ed ++ aade == edaade →→ aeddea

对于这个题,刚开始完全是使用暴力,但是T了两发,很难受,静下心 仔细一想,他所给的n是10^5,如果所有按照所有情况都给遍历一边的话,那就得大约10^10了,必定T。

所以对于这个题的话,当组合成的串中,判断字符相同的个数为单数时为1,个数为偶数或者没有时为0,可以将字符串看成26位的01字符串,判断字符串出现的次数。并且统计次数。


#include<bits/stdc++.h>
#define ll long long
using namespace std;
string str;
map<int ,int>mp;
int n;
ll ans;
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1;i <= n;i++){
        int sum  = 0;
        cin >> str;
        for(int  j = 0;j < str.size();j++){
            int k = str[j] - 'a';
            sum ^= (1 << k);
        }
        ans += mp[sum];   
        for(int j = 0;j < 26;j++){
            ans += mp[sum ^ (1 << j)];
            if(ans) cout<<(sum ^ (1 << j))<<" ";
        }
        mp[sum]++;
    }
    cout <<ans<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值