CodeForces 1045I --- Palindrome Pairs

题目来源:Problem - 1045I - Codeforces

题目简介:

                                                                     Palindrome Pairs

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 NN (1≤N≤1000001≤N≤100000), representing the length of the input array.

Eacg of the next NN 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 10000001000000.

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

在讲这道题之前首先要引入map的概念:

  1. map简介
  1. map可以将任何基本类型映射到任何基本类型
  2. map提供一对一的数据处理,第一个称为关键字,第二个为关键字的值
  3. map内部自动排序
  4. map容器内元素可以通过下标访问如map[‘a’]=2;

  

map常用语法

Map.insert()插入

   Map.find()查找一个元素

Map.erase()删除一个元素(运用迭代器,定义方式map<int,int>iterator it)

Map.size()长度

解题思路:我们首先定义一个map数组tmp,存储字符串中每个字符出现的次数,如果为奇数,则用01串存储并做为第一关键字存入mp数组并累加。观察题目,我们发现两个字符能否构成回文子串条件是奇数位字母最多1种,如果大于等于两种的奇数位字母,它必定不是回文子串

接下来有两种情况,第一种,01串与他本身相同,比如aaabb与abb,01串都为0001,可构成回文子串,总数为n*(n-1)/2

第二种,当01串仅有一位差距,即连接后仍然有1个字母总数位奇数。如有两位及以上差距,则有多个奇数,无法构成字符串

接下来是AC代码以及注释:

#include<iostream>
#include<map>
using namespace std;
int n;
typedef long long ll;

map<ll, ll> mp;

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		map<int, int> tmp;
		tmp.clear();
		for (int j = 0; j < s.length(); j++)
		{
			tmp[s[j] - 'a']++;//存储字符串中每个字符出现的次数。
		}
		ll x = 0;
		for (int j = 0; j < 26; j++)
		{
			if (tmp[j] % 2)
			{
				x = x + (ll(1 << j));//用01串存储每个奇数字符,如aba,b为奇数,01串为10.
			}
		}
		mp[x]++;
	}
	long long ans = 0;
	for (auto i : mp)
	{
		ll h = i.first;
		ans += (mp[h] - 1) * mp[h] / 2;//01串相同时,比如aaabb与abb,01串都为0001,可构成回文子串,总数为n*(n-1)/2
		for (int j = 0; j < 26; j++)
		{
			if (h >> j & 1)
			{
				ll t = h ^ ((ll)1 << j);
				ans += mp[h] * mp[t];//当01串仅有一位差距,即连接后仍然有1个字母总数位奇数。如有两位及以上差距,则有多个奇数,无法构成字符串
			}
		}
	}
	cout << ans << endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值