关于这个代码的复杂度可不可过
  • 板块学术版
  • 楼主Limitless_lmw
  • 当前回复15
  • 已保存回复16
  • 发布时间2024/8/20 09:05
  • 上次更新2024/8/20 11:19:59
查看原帖
关于这个代码的复杂度可不可过
809765
Limitless_lmw楼主2024/8/20 09:05
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#pragma GCC optimize("Ofast")
using namespace std;

string s[30];
int n;
bool c[30];
int ans=0;

int main(){
//	freopen("font.in","r",stdin);
	//freopen("font.out","w",stdout);
	cin>>n;
	for(int i = 1; i<=n; i++) cin>>s[i];
	for(int i = 0; i<(1<<n); i++){
		memset(c,0,sizeof c);
		string t;
		for(int j = 1; j<=n; j++){
			if(i>>(j-1)&1){
				t+=s[j];
			}
		}
//		cout<<t<<endl;
		int cnt=0;
		for(auto ch:t){
			if(!c[ch-'a'+1]) cnt++,c[ch-'a'+1]=1;
		}
		if(cnt==26){
			ans++;
		}
	}
	cout<<ans;
	return 0;
}

显然是 O(2n×n)O(2^n\times n) 的,数据范围是 n≤25n\le25.

如果不行,怎么优化?

2024/8/20 09:05
加载中...