Codeforces - Palindrome Pairs

题目链接:Codeforces - Palindrome Pairs


因为可以随意排列。

所以,答案只与奇偶性有关。只有一个字母是奇数,或者没有。

进而,我们可以发现“bbb”和“b”其实本质是一样的。所以我们可以对每个字符串状态压缩。

然后枚举哪一位是1即可。

因为状态很大,所以我们可以对状态离散化,或者直接用map存。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int st[N],n;	char str[N*10];	long long res;
unordered_map<int,int> num;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%s",str+1);
		for(int j=1;str[j];j++)	st[i]^=(1<<(str[j]-'a'));
	}
	for(int i=1;i<=n;i++){
		res+=num[st[i]];
		for(int j=0;j<26;j++)	res+=num[st[i]^(1<<j)];
		num[st[i]]++;
	}
	cout<<res;
	return 0;
}
发布了604 篇原创文章 · 获赞 242 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104392167
今日推荐