D. Problem with Random Tests

Problem - 1743D - Codeforces

 

思路:因为是或,所以答案一定会比原串更大,并且为了保留更多的1,我们可以选择原串作为其中一个串,另一个串则要找到第一个为0的位置,我们希望让这个为1,为了让这个位置在或之后为1,需要满足两个条件,假设这个位置为id,那么首先要满足长度至少为n-id+1,并且这个串的最高位还要是1,所以满足条件的起始位置一定在第一个1到第一个1之后的第一个0之前,而这之间有30个1的概率为1/2^(30)所以基本不可能,所以可以直接进行暴力

这个题数据随机是一个关键点,因为在数据随机的情况下,每个位置为1的概率为1/2,所以连续的1不会很长,那么就可以暴力的做

// Problem: D. Problem with Random Tests
// Contest: Codeforces - Educational Codeforces Round 137 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1743/D
// Memory Limit: 512 MB
// Time Limit: 4000 ms

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<bitset>
#include<deque>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<queue>
#include<map>
#include<stack>
#include<vector> 
#include<set>
#include<cstdlib>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=1e6+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
string str;

void check(int id,int len,string &ans,string temp,int r) {
	string s1=temp.substr(id,len);
	string s2=ans.substr(r,len);
	string s3=temp.substr(r,len);
	
	for(int i=0;i<s1.size();i++) {
		if(s3[i]=='1') s1[i]='1';
	}
	
	if(s1>s2) {
		string p=ans.substr(0,temp.size()-len)+s1;
		ans=p;
	}
}

void solve() {
	n=read();
	cin>>str;
	
	int l=0;
	while(l<n&&str[l]=='0') l++;
	
	if(l>=n) printf("0\n");
	else {
		string ans=str.substr(l,n-l);
		int r=0;
		while(r<ans.size()&&ans[r]=='1') r++;

		if(r>=ans.size()) cout<<ans<<'\n';
		else {
			string temp=ans;
			int len=ans.size()-r;
			for(int i=0;i<r;i++) {
				check(i,len,ans,temp,r);
			}
			
			cout<<ans<<'\n';
		}
	}
}

int main() {
    // init();
    // stin();
	//ios::sync_with_stdio(false); 

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值