C. Sum of Substrings题解
C. Sum of Substrings
题目大概意思,给你一个01串,求和最小,其中和是该串所有相邻字符所组成的十进制数的和。
如:0110, sum = 01 + 11 + 10 = 22。
通过观察我们可以发现,除了第一个位置和最后一个位置,其他位置上的1对和的贡献都是11。
所以我们只需要特殊考虑挪1到第一个和最后一个位置上。
题目没说必须挪,所以我们可以不用挪。
另外,注意只有一个1的情况!
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t, n, k;
char st[N];
signed main(){
cin >> t;
while(t --){
cin >> n >> k;
memset(st, 0, sizeof st);
cin >> st;
int pos1 = 0, pos2 = 0;
int cnt = 0;
bool flag = false;
for(int i = 0; i < strlen(st); i++){
if(pos1 == 0 && st[i] == '1' && flag == false){
pos1 = i;
flag = true;
}
if(st[i] == '1') cnt ++;
}
for(int i = n - 1; i >= 0; i--){
if(pos2 == 0 && st[i] == '1'){
pos2 = i;
break;
}
}
if(cnt == 0){
cout << 0 << endl;
continue;
}
if(k == 0){
int res = 0;
if(st[0] == '1') res = 10, cnt --;
if(st[n - 1] == '1') res += 1, cnt --;
cout << res + cnt * 11 << endl;
continue;
}
if(cnt == 1){
if(k >= n - pos2 - 1){
cout << 1 << endl;
}
else if(k >= pos1){
cout << 10 << endl;
}
else cout << 11 << endl;
continue;
}
int ans = 0;
if(pos2 == n - 1){
ans = 1;
cnt --;
}
else{
if(k >= n - pos2 - 1){
k -= n - pos2 - 1;
ans += 1;
cnt --;
}
}
if(pos1 == 0){
ans += 10;
cnt --;
}
else{
if(k >= pos1){
ans += 10;
cnt --;
}
}
cout << ans + 11 * cnt << endl;
}
return 0;
}
C. Sum of Substrings题解的更多相关文章
- CF 1400F x-prime Substrings 题解【AC自动机+DP】
CF 1400F.x-prime Substrings 题意: 给定一个由\('1'\)到\('9'\)组成的字符串\(s\)和一个数\(x\),定义一个串为\(x-prime\)串,当且仅当这个串上 ...
- [LeetCode] Range Sum Query - Mutable 题解
题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...
- Educational Codeforces Round 37-F.SUM and REPLACE题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...
- CF102B Sum of Digits 题解
Content 给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和.问你几次操作之后可以将 \(n\) 变为一位数. 数据范围:\(1\leqslant n\leqsla ...
- POJ3415:Common Substrings——题解
http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...
- SPOJ8222/NSUBSTR:Substrings——题解
https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...
- SPOJ694/DISUBSTR:Distinct Substrings——题解
https://vjudge.net/problem/SPOJ-DISUBSTR https://www.luogu.org/problemnew/show/SP694 http://www.spoj ...
- CF1139A Even Substrings 题解
Content 有一个长度为 \(n\) 的数字串 \(s\),试求出代表偶数的子串个数. 数据范围:\(1\leqslant n\leqslant 65000\),\(s\) 仅包含数字 \(1\s ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
随机推荐
- 配置Chrome支持网页内的frame跨域
前言 跨域限制可以保证安全,但是调试的时候关掉会更方便,然而现在网络上能找到的关闭跨域限制方法,在新版的Chrome浏览器上根本没用-- 经过一番摸索,发现用旧版的Chrome就可以绕过跨域限制,刚好 ...
- [转帖]shell编程:shell变量的核心基础知识与实战(二)
shell编程:shell变量的核心基础知识与实战(二) https://www.cnblogs.com/luoahong/articles/9152039.html Shell 变量类型 变量可以分 ...
- [转帖]Docker资源(CPU/内存/磁盘IO/GPU)限制与分配指南
https://zhuanlan.zhihu.com/p/417472115 什么是cgroup? cgroups其名称源自控制组群(control groups)的简写,是Linux内核的一个功能, ...
- [转帖]harbor-db restarting问题
现象: 在安装harbor后,启动时发现harbor-db 一直是restarting,/harbor-jobservice,/harbor-core 这两是starting 状态,如下图 解决: 1 ...
- [转帖]Python基础之文件处理(二)
https://www.jianshu.com/p/7dd08066f499 Python基础文件处理 python系列文档都是基于python3 一.字符编码 在python2默认编码是ASCII, ...
- [转帖]A17再次证明苹果才是王者,组装芯片的安卓手机给它提鞋都不配
http://news.sohu.com/a/653472711_121124371 在挤了两代牙膏之后,苹果终于拿出了性能大幅提升的A17处理器,外媒传出A17处理器的性能提升幅度至少超过四成,相比 ...
- Chrome 历史版本下载点
https://www.chromedownloads.net/chrome64win-stable/
- vue过滤器(filter)的使用
过滤器分全局过滤器和局部过滤器 <div id="app"> <p>电脑价格:{{price | addPriceIcon}}</p> < ...
- 如何在 Linux 上使用 NPOI
由于 NPOI 使用 System.Drawing.Common,因此在 Linux 系统上必须安装 libgdiplus 和 libc6. Ubuntu 16.04+ apt-get install ...
- 19.10 Boost Asio 同步文件传输
在原生套接字编程中我们介绍了利用文件长度来控制文件传输的方法,本节我们将采用另一种传输方式,我们通过判断字符串是否包含goodbye lyshark关键词来验证文件是否传输结束了,当然了这种传输方式明 ...