Dasha and Nightmares Dasha 和噩梦

time limit per test

4 seconds

memory limit per test

512 megabytes

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〉.

每次测试时间限制 4 秒

每次测试内存限制 512 兆字节

Dasha 是一名优秀的学生,正在全国最好的数学学院学习。最近,一位神秘的陌生人将 n

个由小写拉丁字母 s1、s2、...、sn

组成的单词带到了学院。从那天起,Dasha 就一直被噩梦折磨。

考虑一对整数 〈i,j〉

(1≤i≤j≤n

)。噩梦是一个字符串,它为真:

它由连接 sisj 获得

它的长度是奇数;

其中不同字母的数量正好是 25

单词中每个字母出现的次数是奇数。

例如,如果 si=

"abcdefg" 和 sj=

"ijklmnopqrstuvwxyz",则对 〈i,j〉

会造成噩梦。

如果 Dasha 能数出噩梦的数量,她就不会再做噩梦了。噩梦太多了,所以 Dasha 需要你的帮助。数一数不同噩梦的数量。

如果对应的对 〈i,j〉

不同,则称噩梦不同。如果 i1≠i2

或 j1≠j2

,则对 〈i1,j1〉

和 〈i2,j2〉

不同。

输入

第一行包含一个整数 n

(1≤n≤2⋅105

) --- 单词数。

接下来的 n

行包含单词 s1,s2,...,sn

,由小写拉丁字母组成。

保证单词的总长度不超过 5⋅106

输出

打印一个整数------不同噩梦的数量。

示例

输入副本

10

ftl

abcdefghijklmnopqrstuvwxy

abcdeffghijkllmnopqrsttuvwxy

ffftl

aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy

thedevid

bcdefghhiiiijklmnopqrsuwxyz

gorillasilverback

abcdefg

ijklmnopqrstuvwxyz

输出副本

5

注意

在第一个测试中,噩梦是由〈1,3〉

、〈2,5〉

、〈3,4〉

、〈6,7〉

、〈9,10〉

组成的。

代码:

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;

const long long MAX_N = 200010;
const long long FULL_MASK = (1 << 26) - 1;

unordered_map<long long, long long> count_map;

long long solve() {
    long long n;
    cin >> n;

    vector<string> strings(n + 1);
    vector<long long> bitmask(n + 1), all_chars(n + 1);

    for (long long i = 1; i <= n; ++i) {
        cin >> strings[i];
        for (char c : strings[i]) {
            bitmask[i] ^= (1 << (c - 'a'));
            all_chars[i] |= (1 << (c - 'a'));
        }
    }

    long long ans = 0;

    for (long long excluded_char = 0; excluded_char < 26; ++excluded_char) {
        count_map.clear();

        for (long long i = 1; i <= n; ++i) {
            if ((all_chars[i] >> excluded_char) & 1) continue;

            long long target = bitmask[i] ^ FULL_MASK ^ (1 << excluded_char);
            ans += count_map[target];
            count_map[bitmask[i]]++;
        }
    }

    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cout << solve() << endl;

    return 0;
}
相关推荐
汉汉汉汉汉1 小时前
C++11新特性详解:从列表初始化到线程库
c++
楼田莉子2 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
大锦终3 小时前
【算法】模拟专题
c++·算法
方传旺3 小时前
C++17 std::optional 深拷贝 vs 引用:unordered_map 查询大对象性能对比
c++
Dontla4 小时前
Makefile介绍(Makefile教程)(C/C++编译构建、自动化构建工具)
c语言·c++·自动化
何妨重温wdys4 小时前
矩阵链相乘的最少乘法次数(动态规划解法)
c++·算法·矩阵·动态规划
重启的码农4 小时前
ggml 介绍 (6) 后端 (ggml_backend)
c++·人工智能·神经网络
重启的码农4 小时前
ggml介绍 (7)后端缓冲区 (ggml_backend_buffer)
c++·人工智能·神经网络
雨落倾城夏未凉4 小时前
5.通过拷贝构造函数复制一个对象,假如对象的成员中有个指针类型的变量,如何避免拷贝出来的副本中的该成员之下行同一块内存(等价于默认拷贝构造函数有没有缺点)
c++·后端
雨落倾城夏未凉4 小时前
4.深拷贝VS浅拷贝
c++·后端