CF1800F Dasha and Nightmares 题解

分析

考虑枚举。

注意到第二个条件是必须要有 25 25 25 个字符在里面出现过,故考虑枚举唯一没出现过的字符 k k k,然后再枚举 s i s_i si

c n t i , j cnt_{i,j} cnti,j 表示 s i s_i si 中字符 c c c 出现的奇偶性。如果有字符 c ≠ k ∧ c n t i , c = 0 c \ne k \land cnt_{i,c}=0 c=kcnti,c=0,则在 s j s_j sj 中必有 c n t j , c = 1 cnt_{j,c}=1 cntj,c=1;反之同理。

枚举字符 c c c,可以得到要与 s i s_i si 匹配的字符串 s j s_j sj 中所有字符出现次数的奇偶情况。把这玩意看成二进制拿个桶记录答案就行了。复杂度 O ( a 2 n ) O(a^2n) O(a2n) a = 26 a=26 a=26

注:桶里的东西不能在 k k k 之外预处理,因为可能有 c n t i , k = 0 cnt_{i,k}=0 cnti,k=0 k k k s i s_i si 中出现过的情况。这样会使你答案大很多。对于每次桶的清空,如果直接枚举所有状态复杂度会高不止一点。发现我们放桶里面的二进制值最多有 n n n 个,所以随便拿个东西存一下往那些桶放了值即可。

代码

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define re register
#define il inline
#define PII pair<int,int>
#define x first
#define y second

il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}

const int N=2e5+10,M=(1<<26);
int n;
bool cnt[N][26],cnt2[N][26];
int Cnt[N],num[M];
vector<int> d;
long long ans;

il void solve(){
	n=read();
	for(re int i=1;i<=n;++i){
		string s;cin>>s;
		for(re int j=0;j<s.size();++j) cnt[i][s[j]-'a']^=1,cnt2[i][s[j]-'a']=1;
		for(re int j=0;j<26;++j) Cnt[i]+=(cnt[i][j]*(1<<j));
	}
	for(re int k=0;k<26;++k){
		for(re int i=1;i<=n;++i)
		if(!cnt2[i][k]){
			int now=0;
			for(re int c=0;c<26;++c) if(!cnt[i][c]&&c!=k) now+=(1<<c);
			ans+=num[now],++num[Cnt[i]],d.push_back(Cnt[i]);
		}
		for(re int i=0;i<d.size();++i) --num[d[i]];
		d.clear();
	}
	printf("%lld\n",ans);
	return ;
}

signed main(){
//	int t=read();while(t--)
	solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

harmis_yz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值