F. Dasha and Nightmares

Dasha, an excellent student, is studying at the best mathematical lyceum in the country. Recently, a mysterious stranger brought nn words consisting of small latin letters s1,s2,…,sns1,s2,…,sn to the lyceum. Since that day, Dasha has been tormented by nightmares.

Consider some pair of integers 〈i,j〉〈i,j〉 (1≤i≤j≤n1≤i≤j≤n). A nightmare is a string for which it is true:

  • It is obtained by concatenation sisjsisj;
  • Its length is odd;
  • The number of different letters in it is exactly 2525;
  • The number of occurrences of each letter that is in the word is odd.

For example, if si=si= "abcdefg" and sj=sj= "ijklmnopqrstuvwxyz", the pair 〈i,j〉〈i,j〉 creates a nightmare.

Dasha will stop having nightmares if she counts their number. There are too many nightmares, so Dasha needs your help. Count the number of different nightmares.

Nightmares are called different if the corresponding pairs 〈i,j〉〈i,j〉 are different. The pairs 〈i1,j1〉〈i1,j1〉 and 〈i2,j2〉〈i2,j2〉 are called different if i1≠i2i1≠i2 or j1≠j2j1≠j2.

Input

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of words.

The following nn lines contain the words s1,s2,…,sns1,s2,…,sn, consisting of small latin letters.

It is guaranteed that the total length of words does not exceed 5⋅1065⋅106.

Output

Print a single integer — the number of different nightmares.

Example

input

Copy

 
  

10

ftl

abcdefghijklmnopqrstuvwxy

abcdeffghijkllmnopqrsttuvwxy

ffftl

aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy

thedevid

bcdefghhiiiijklmnopqrsuwxyz

gorillasilverback

abcdefg

ijklmnopqrstuvwxyz

output

Copy

5

Note

In the first test, nightmares are created by pairs 〈1,3〉〈1,3〉, 〈2,5〉〈2,5〉, 〈3,4〉〈3,4〉, 〈6,7〉〈6,7〉, 〈9,10〉〈9,10〉.

思路

题目要求两个字符串相加他们中小写字母出现的次数等于25

字符串长度恰好是奇数

每个字母出现的次数恰好是奇数

比赛时写的时候没有发现他们中的一些联系,比赛后看别人的代码才发现的

就是出现25个奇数的话他们的长度也就是奇数,而奇数只能由奇数+偶数相加得来的

所以当一个字符串中如果某个字母出现偶数次的话那么一定要有一个出现奇数次的与他相加

就是说一个字母出现偶数次他在这两串合并并不起多大的作用

就相当于没有出现也可以说出现0次

然后恰好25个枚举某个数没出现过

然后o(n)循环就可以求出来了

#include
#define ll long long
#define PII pair
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f, N = 2e5 + 5, mod = 1e9 + 7;
int f[1 << 26];//状态压缩
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);

	int n;
	cin >> n;
	vectors(n);
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	ll ans = 0;
	std::vector> cnt(n);
	for (int i = 0; i < n; i++) {
		cnt[i] = vector(26);
	}
	vectormask(n);
	for (int i = 0; i < n; i++) {
		for (auto c : s[i]) {
			cnt[i][c - 'a'] += 1;
		}
		for (int j = 0; j < 26; j++) {
			mask[i] |= (cnt[i][j] % 2) << j;
		}
	}
	for (int c = 0; c < 26; c++) {
		for (int i = 0; i < n; i++) {
			if (cnt[i][c] == 0) {
				ans += f[(1 << 26) - 1 - (1 << c) - mask[i]];
				f[mask[i]] += 1;
			}
		}
		for (int i = 0; i < n; i++) {
			if (cnt[i][c] == 0) {
				f[mask[i]] -= 1;
			}
		}
	}
	cout << ans << '\n';
}
 

你可能感兴趣的:(c++)