期望 + 暴力

[Problem - D - Codeforces](https://codeforces.com/contest/1743/problem/E)

题意

给出一个长度为 \(n\;(1<=n<=10^6)\) 的字符串 \(s\), 选取两个 \(s\) 的子串 \(a,b\), 使得 \(a\;or\;b\) 的值最大

保证 \(s\) 中每个位置出现 0 或 1 都是等概率的

思路

  1. 把 \(s\) 的前导 0 去掉,这些位置无法变成 1
  2. \(a=s\) 一定是最优的,\(b\) 的选取是尽量把 \(a\) 的前面的 0 给补上
  3. 设 idx 为 \(a\) 中第一个 \(0\) 的位置,\(b\) 的开头一定是在 idx 之前,这样才能补上 idx 这个位置上的 0
  4. 从 0 到 idx - 1 暴力枚举 b 的开头即可,因为每个位置出现 0/1 是等概率的,所以 idx 不会很大

代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; string s;
int n, t, cnt;
const int N = 110;
int pos[N], d[N];
vector<int> vt; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> s;
int idx = n;
for (int i = 0; i < n; i++)
{
if (s[i] == '1')
{
idx = i;
break;
}
}
if (idx == n)
{
cout << 0 << endl;
return 0;
}
s = s.substr(idx);
n = s.size();
for (int i = 0; i < n; i++)
{
if (s[i] == '0')
{
idx = i;
break;
}
}
string ans = s;
for (int i = 0; i < idx; i++)
{
string now = s;
for (int j = 0; idx + j < n; j++)
{
if (s[i + j] == '1')
now[idx + j] = '1';
}
ans = max(ans, now);
}
cout << ans << endl;
return 0;
}

最新文章

  1. Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
  2. 深入解析SQL Server并行执行原理及实践(上)
  3. 解决oracle 中文入库是乱码的问题
  4. 【原】js检测移动端横竖屏
  5. [C#] AY.WPF-图形编程-高中生为起点-研究报告1
  6. IOCP 浅析
  7. Integer cache
  8. 学Lua(上)
  9. 动态网页技术---JSP
  10. Js设置所有连接是触发/swt/的代码
  11. django模板 实现奇偶分行
  12. 关于Webapp的注意事项
  13. 一个初学所了解的jquery事件
  14. Android -- onMeasure()源码分析
  15. 初次使用Python脚本,proto协议文件转Lua代码
  16. 手把手教你用.NET Core写爬虫
  17. 心路历程(一)-自学java两个月心得
  18. JS 中 原生方法 (三) --- Date 日期
  19. redis4.0 cluster搭建
  20. js 原型原型链

热门文章

  1. CTFshow——funnyrsa1的wp理解
  2. 网络监测工具之Zabbix的搭建与测试方法(三) ---Zabbix Agent
  3. Hello 2023 A-D
  4. 软件安装——tortoiseGit安装和配置
  5. [Unity]Update()与FixedUpdate()
  6. 刷题笔记——2181.信息学奥赛一本通T1005-地球人口承载力估计
  7. 《机器人SLAM导航核心技术与实战》第1季:第4章_机器人传感器
  8. liunx系统安装JDK环境详细步骤
  9. VBA中的(升降序)排名问题
  10. CSS-@规则(At-rules)常用语法使用总结