CF1800F Dasha and Nightmares 题解

分析

考虑枚举。

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

\(cnt_{i,j}\) 表示 \(s_i\) 中字符 \(c\) 出现的奇偶性。如果有字符 \(c \ne k \land cnt_{i,c}=0\),则在 \(s_j\) 中必有 \(cnt_{j,c}=1\);反之同理。

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

注:桶里的东西不能在 \(k\) 之外预处理,因为可能有 \(cnt_{i,k}=0\)\(k\)\(s_i\) 中出现过的情况。这样会使你答案大很多。对于每次桶的清空,如果直接枚举所有状态复杂度会高不止一点。发现我们放桶里面的二进制值最多有 \(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;
}
posted @ 2024-03-05 19:04  harmis_yz  阅读(18)  评论(0)    收藏  举报