跳到正文

题解

UVA1326

RainRecall约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年9月29日
收录
2025年12月12日
更新
2026年9月12日
篇幅
约 2 分钟
获赞
2
题目
UVA1326

版本

  1. v1当前

拿到题目考虑搜索,但是复杂度 O(Tn×2n)O(Tn\times 2^n),虽然 nn 最大只有 2424,但是多组数据,还是会炸。

但是看到 2424 和偶数这两个东西,我们想到把每个字符串压成 22 进制。那如何判断是偶数呢?

我们先把每个字符串字母出现的次数偶数设为 00,奇数为 11。我们又知道,一个数异或自己偶数次是 00。这样就好办了,我们暴力搜索每个数字判断选不选,把它们异或起来,最后结果如果是 00 就统计答案即可。

时间复杂度 O(T×2n)O(T\times2^n),足以通过本题。

AC code

C++
#include<iostream>
using namespace std;
string s[30]; 
int n,a[30],b[30],ans,qwq[30],cnt,ttq[30];
void dfs(int x,int sum){
	if(x>n){
		if(sum==0&&cnt>ans){
			ans=cnt;
			for(int i=1;i<=cnt;++i)ttq[i]=qwq[i];
		}
		return;
	}
	qwq[++cnt]=x;
	dfs(x+1,sum^a[x]);
	--cnt;
	dfs(x+1,sum);
}
int main(){
	while(cin>>n){
		ans=0;
		for(int i=1;i<=n;++i)ttq[i]=a[i]=cnt=0;
		for(int i=1;i<=n;++i){
			cin>>s[i];
			for(int j=0;j<26;++j)b[j]=0;
			int l=s[i].size();
			for(int j=0;j<l;++j)b[s[i][j]-'A']++;
			for(int j=0;j<26;++j)a[i]=(a[i]<<1)+(b[j]&1);
		}
		dfs(1,0);
		cout<<ans<<'\n';
		for(int i=1;i<=ans;++i)cout<<ttq[i]<<" ";
		cout<<'\n';
	}
	return 0;
}
原文
存档评论1 条抓取于