当前位置:网站首页>C. Binary String(求前缀和)
C. Binary String(求前缀和)
2022-07-21 05:04:00 【这一wa是晚安】
You are given a string ss consisting of characters 0 and/or 1.
You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:
- the number of characters 0 left in the string;
- the number of characters 1 removed from the string.
What is the minimum cost of removal you can achieve?
Input
The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.
Each test case consists of one line containing the string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105), consisting of characters 0 and/or 1.
The total length of strings ss in all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print one integer — the minimum cost of removal you can achieve.
Example
input
Copy
5 101110110 1001001001001 0000111111 00000 1111output
Copy
1 3 0 0 0Note
Consider the test cases of the example:
- in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is 11;
- in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is 33;
- in the third test case, it's optimal to remove four characters from the beginning;
- in the fourth test case, it's optimal to remove the whole string;
- in the fifth test case, it's optimal to leave the string as it is.
题意:
给你一个01字符串,你可以从前或者从后删除一些字符串,那么你最后的成本将是你删除过后的字符串中剩余0的数量,或者是你删除过程中删除1的数量,两者中的最大值 。
思路:
我们先想一下最优的效果,我们尽可能让1留下,让0自行被删除 。
所以我们首先统计字符串中0的个数。
因为我们最优得效果就是全部.剩下的是1,我们设0的个数是x,字符串的长度是l所以我们挨着遍历(l-x)的每个区间剩下0的个数的最小值就是我们的答案
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
#define int long long
using namespace std;
const int N = 2e5 + 10;
string s;
int v[N];
void solve()
{
cin >> s;
int len = s.size();
for(int i = 1; i <= len; i ++)
{
v[i] = v[i-1]+(s[i-1] == '0');
//cout << i << " : " << v[i] << endl;
}
int ans = 1e9;
//j=len-v[n] ???????
for(int i = 1, j = len - v[len]; i <= len && j <= len; i ++, j++)
ans = min(ans, v[j]-v[i-1]);
//
cout << ans << '\n';
}
signed main()
{
IOS;
int T;
cin >> T;
while(T--)
solve();
return 0;
}
边栏推荐
- UNet复现及环境配置(含数据集)
- 快捷键、命令
- 基于STM32F407的摄像头(不带FIFO的OV7670)图像采集及LCD显示实验-笔记整理
- 猫狗图片资源
- Jenkins plug-in development - provide external access interface
- Mask RCNN loading weight error
- Starfish OS: create a new paradigm of the meta universe with reality as the link
- Is it really necessary to add interfaces to each class in the service layer and Dao layer
- Annotation related learning
- pycharm 笔记
猜你喜欢
初学谷歌bert模型的预训练和fine-tuning微调
数据库连接不上
Tensorflow GPU environment configuration
Starfish OS: create a new paradigm of the meta universe with reality as the link
Pytorch installation
[record] the operation of optisystem is stuck, and it is unable to click to close, input variable values and other solutions
[3D modeling] SolidWorks 3D modeling and prusaslicer slice printing learning notes
Detailed explanation of NiO channel
Semantic segmentation, instance segmentation
使用Arduino搭建基于阿里云平台的物联网智能家居
随机推荐
深度学习源码项目里的checkpoint
Pre training and fine tuning of Google Bert model for beginners
4.paddlepaddle之10行代码mnist手写数字识别
4. 10 lines of code MNIST handwritten numeral recognition of paddlepaddle
PyTorch的模型定义
(笔记)吴恩达深度学习L4卷积神经网络W1
【C语言】一些常用的字符串函数--学习笔记
Loopback input and loopback output
Owncloud 9.0 better cross server sharing and scalability
pycharm专业版创建flask项目|下载flask包|以及一些例子
[PCB] Based on stm32f103rct6 rocker - Bluetooth Module Development Board - drawing Board note arrangement
OPT101单片光电二极管和单电源互阻放大器使用说明
Isempty and isblank
Esp8266 firmware download and burning (include at firmware download address + firmware burning precautions)
Self attention principle
Pycharm notes
枚举的例子
Es aggregate statistical syntax
Checkpoint in the deep learning source code project
回型输入和回型输出