D.最大字符集
题目链接-D.最大字符集



解题思路
- 需特判一下 n = 1 n=1 n=1和 n = 2 n=2 n=2的情况, n = 1 n=1 n=1时,集合可以是{1}或{0}, n = 2 n=2 n=2时,集合可以是{0,11}或{1,00}
- n > 2 n>2 n>2时,集合中元素都应100…001的结构,即{11,101,1001,…}
- 具体操作见代码
附上代码
//#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
if(n==1){
cout<<1<<endl<<1<<endl;
return 0;
}
if(n==2){
cout<<2<<endl;
cout<<0<<endl<<11<<endl;
return 0;
}
cout<<n-1<<endl;
for(int i=2;i<=n;i++){
cout<<1;
for(int j=2;j<i;j++)
cout<<0;
cout<<1<<endl;
}
return 0;
}
本文探讨了在特定条件下构建最大字符集的算法问题。针对不同输入值n,阐述了特判情况及通用解法,当n大于2时,提出了以1开头后跟0重复的字符串构成集合的解决方案,并附带了实现代码。

466

被折叠的 条评论
为什么被折叠?



