D. Problem with Random Tests

You are given a string ss consisting of nn characters. Each character of ss is either 0 or 1.

A substring of ss is a contiguous subsequence of its characters.

You have to choose two substrings of ss (possibly intersecting, possibly the same, possibly non-intersecting — just any two substrings). After choosing them, you calculate the value of the chosen pair of substrings as follows:

  • let s1s1 be the first substring, s2s2 be the second chosen substring, and f(si)f(si) be the integer such that sisi is its binary representation (for example, if sisi is 11010, f(si)=26f(si)=26);
  • the value is the bitwise OR of f(s1)f(s1) and f(s2)f(s2).

Calculate the maximum possible value you can get, and print it in binary representation without leading zeroes.

Input

The first line contains one integer nn — the number of characters in ss.

The second line contains ss itself, consisting of exactly nn characters 0 and/or 1.

All non-example tests in this problem are generated randomly: every character of ss is chosen independently of other characters; for each character, the probability of it being 1 is exactly 1212.

This problem has exactly 4040 tests. Tests from 11 to 33 are the examples; tests from 44 to 4040 are generated randomly. In tests from 44 to 1010, n=5n=5; in tests from 1111 to 2020, n=1000n=1000; in tests from 2121 to 4040, n=106n=106.

Hacks are forbidden in this problem.

Output

Print the maximum possible value you can get in binary representation without leading zeroes.

Examples

input

Copy

5
11010

output

Copy

11111

题意:给你一个二进制字符串s

让你选两个子串s1 s2

使两个子串进行按位或运算之后的二进制形式的值最大,并输出

思路:因为要尽可能大所以s1要尽可能长,并且最高位为1

那么我们找到第一个是1的字符当做s1的开头

那么对于第二个字符串来说就要把s1是0的位置尽可能用1进行按位与运算

那么s2的长度就是s1里第一个0到最后一个0的长度len

设s1里第一个1的坐标为id1,第一个0的位置是id2,那么id1~id2-1的位置上都是1

那么我们就枚举开头为id1~id2-1,长度为len的字符串,

每一位与一下看最大字典序就可以了

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b) {
	return b? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
const int N=2e5+10;
int n;
int a[N];
void sove(){
	cin>>n;
	string s;
	cin>>s;
	s=" "+s;
	int id1=-1,id2=-1;
	int con=0;
	int tmp=-1;
	for(int i=1;i<=n;i++){
		if(s[i]=='1'){
			tmp=i;
			break;
		}
	}
	if(tmp==-1){
		cout<<0<<endl;
		return ;
	}
	for(int i=tmp;i<=n;i++){
		if(s[i]=='0'){
			if(id1==-1){
				id1=i;
				id2=i;
			}
			
			else id2=i;
		}
		
	}
	int len=id2-id1+1;
//	cout<<"id1=="<<id1<<" "<<"id2=="<<id2<<endl;
	vector<string> q;
	for(int i=tmp;i<id1;i++){//枚举开头
		string f;//记录与后的字符串
		for(int j=0;j<len;j++){//每一位进行与运算
			if(s[i+j]=='1'||s[id1+j]=='1')f+='1';
			else f+='0';
		}
		q.push_back(f); 
	}
	sort(q.begin() ,q.end() );//进行排序
	int mm=q.size() ;
	for(int i=tmp;i<id1;i++)cout<<s[i];
	cout<<q[mm-1];//输出字典序最大的与之后的字符串
	for(int i=id2+1;i<=n;i++)cout<<s[i];
	cout<<endl;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
//	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值