【CF1800F】Dasha and Nightmares

Dasha and Nightmares

题面翻译

给定 n n n n n n 个字符串 s i s_i si

问有多少对不同的 ⟨ i , j ⟩ ( 1 ≤ i ≤ j ≤ n ) \langle i,j\rangle(1\leq i\leq j\leq n) i,j(1ijn),使得 s i s_i si s j s_j sj 拼接后的字符串满足下列条件:

  • 长度为奇数
  • 恰好出现 25 25 25 个字母
  • 每个字母出现次数为奇数

1 ≤ n ≤ 2 × 1 0 5 , ∑ i = 1 n ∣ s i ∣ ≤ 5 × 1 0 6 1\leq n\leq2\times10^5,\sum\limits_{i=1}^n \left\vert s_i\right\vert\leq5\times10^6 1n2×105,i=1nsi5×106

by @Larryyu

题目描述

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 $ s_1, s_2, \ldots, s_n $ to the lyceum. Since that day, Dasha has been tormented by nightmares.

Consider some pair of integers $ \langle i, j \rangle $ ( $ 1 \le i \le j \le n $ ). A nightmare is a string for which it is true:

  • It is obtained by concatenation $ s_{i}s_{j} $ ;
  • 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 $ s_i= $ “abcdefg” and $ s_j= $ “ijklmnopqrstuvwxyz”, the pair $ \langle i, j \rangle $ 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 $ \langle i, j \rangle $ are different. The pairs $ \langle i_1, j_1 \rangle $ and $ \langle i_2, j_2 \rangle $ are called different if $ i_1 \neq i_2 $ or $ j_1 \neq j_2 $ .

输入格式

The first line contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of words.

The following $ n $ lines contain the words $ s_1, s_2, \ldots, s_n $ , consisting of small latin letters.

It is guaranteed that the total length of words does not exceed $ 5 \cdot 10^6 $ .

输出格式

Print a single integer — the number of different nightmares.

样例 #1

样例输入 #1

10
ftl
abcdefghijklmnopqrstuvwxy
abcdeffghijkllmnopqrsttuvwxy
ffftl
aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy
thedevid
bcdefghhiiiijklmnopqrsuwxyz
gorillasilverback
abcdefg
ijklmnopqrstuvwxyz

样例输出 #1

5

提示

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

核心思路

定x[i]可知x[j]
满足x[i]^x[j] = 25

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
string s[maxn];
unordered_map<int,long long> cnt;
long long Cnt;
int res[maxn][101];
int x[maxn];
int vis[maxn][39];
int main(){
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++){
		cin>>s[i];
		for(int j = 0;j < s[i].size();j++){
		    res[i][s[i][j]-'a'+1]++;
		    vis[i][s[i][j]-'a'+1] = 1;
		}
		int q = 0;
		for(int j = 1;j <= 26;j++){
			if(res[i][j]%2 == 1){
			    q = q+(1<<(j-1));
			}
		}
		x[i] = q;
	}
    
    	
		    
        for(int j = 0;j < 26;j++){
        	cnt.clear();
        	for(int i = 1;i <= n;i++){
        		if(res[i][j+1] == 0)cnt[x[i]]++;
			}
        	for(int i = 1;i <= n;i++){
			
        	if(vis[i][j+1] == 1)continue;
        	int q = (1<<j)^(x[i]^((1<<26)-1));//0101 1010
        	Cnt += cnt[q];
            }
		}
	    
	
	cout<<Cnt/2;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值