F. Dasha and Nightmares

F. Dasha and Nightmares

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

Consider some pair of integers i,j (1ijn). A nightmare is a string for which it is true:

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

For example, if si= "abcdefg" and sj= "ijklmnopqrstuvwxyz", the pair 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 are different. The pairs i1,j1 and i2,j2 are called different if i1i2 or j1j2.

Input

The first line contains a single integer n (1n2105) — the number of words.

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

It is guaranteed that the total length of words does not exceed 5106.

Output

Print a single integer — the number of different nightmares.

Example

input

复制代码
10
ftl
abcdefghijklmnopqrstuvwxy
abcdeffghijkllmnopqrsttuvwxy
ffftl
aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy
thedevid
bcdefghhiiiijklmnopqrsuwxyz
gorillasilverback
abcdefg
ijklmnopqrstuvwxyz
复制代码

output

5

Note

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

 

解题思路

  枚举优化题,一般都会用到哈希表。

  首先要发现一个隐藏的性质,就是如果拼接的字符串同时满足只有25个字母和每个字母出现奇数次这两个条件,那么这个拼接的字符串自然就满足长度为奇数的条件(因为奇数乘奇数等于奇数)。因此我们并不需要关注长度为奇数这个条件,而只关注剩下的那两个条件。

  由于我们只关注每个字符串中出现了那些字母以及每个字符出现的奇偶性,而最多就只有26个字母,因此很自然想到用状态压缩,把az映射到025。对于第i个字符串si,定义aisi中出现的字母的状态,如果字母c出现过,那么ai的第c位应该为1,否则为0。定义bi为每个字母出现次数奇偶性的状态,如果字母c出现了奇数次,那么bi的第c位应该为1,否则为0

  对于一个合法的拼接字符串t,很明显tat所表示的状态中应该有251bt所表示的状态中应该也有251,且是1的位置都是一样的,即恰好有一个位为0且是同一个位,假设对应字母c。由于合法的拼接字符串状态少,因此我们就可以枚举c,一共有26个,来表示拼接的字符串中缺少了哪个字母,对应的at=bt=22612c。然后对于每个c,枚举每一个字符串i,很明显si对应的ai的第c位应该为0。然后看看i前面有多少个j,满足aj的第c位为0,且有bjbi=22612c,即bj=bi22612c。因此可以开个哈希表来统计。

  AC代码如下,时间复杂度为O(|s|+26n)

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int a[N], b[N];
 9 int mp[1 << 26];
10 
11 int main() {
12     int n;
13     cin >> n;
14     for (int i = 0; i < n; i++) {
15         string s;
16         cin >> s;
17         for (auto &c : s) {
18             a[i] |= 1 << c - 'a';
19             b[i] ^= 1 << c - 'a';
20         }
21     }
22     LL ret = 0;
23     for (int c = 0; c < 26; c++) {
24         int st = (1 << 26) - 1 - (1 << c);
25         memset(mp, 0, sizeof(mp));
26         for (int i = 0; i < n; i++) {
27             if (~(a[i] >> c) & 1) {
28                 mp[b[i]]++;
29                 ret += mp[st ^ b[i]];
30             }
31         }
32     }
33     cout << ret;
34     
35     return 0;
36 }
复制代码

 

参考资料

  Codeforces Round #855 (Div. 3) F:https://zhuanlan.zhihu.com/p/610978167

  Codeforces Round 855 (Div. 3) Editorial:https://codeforces.com/blog/entry/113477

posted @   onlyblues  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示