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 ...
随机推荐
- [转帖]HTTP X-Forwarded-For 介绍
https://www.runoob.com/w3cnote/http-x-forwarded-for.html X-Forwarded-For 是一个 HTTP 扩展头部.HTTP/1.1(RFC ...
- [转帖]七. PostgreSQL逻辑结构(1)—数据库和模式
https://www.jianshu.com/p/ee8b1bdfdb19 在PostgreSQL里,逻辑结构从高到底依次是:数据库.模式(又叫架构).表.行.当我们打开PostgreSQL官方自带 ...
- [转帖]KingbaseES不同字符类型比较转换规则
https://www.cnblogs.com/kingbase/p/14798059.html Postgresql 常用的字符数据类型的有char.varchar和text,其中 char 固定长 ...
- [转帖]Linux之bash反弹shell原理浅析
环境 攻击机:kali ip:192.168.25.144 靶 机:centos ip:192.168.25.142 过程 kali 监听本地8888端口 靶机 ...
- [转帖]ethtool 命令介绍
https://www.jianshu.com/p/f456e73a0437 name ethtool - query or control network driver and hardware s ...
- Python学习之十三_pip的学习
Python学习之十三_pip的学习 pip的含义 pip: pip is the package installer for Python. You can use pip to install p ...
- [转帖]总结:SpringBoot启动参数配置
一.背景 由于项目中加了bootstrap.properties文件,且文件中有变量,如spring.cloud.config.profile=${spring.profiles.active},而b ...
- [转帖]020 Linux 20 个宝藏命令案例
https://my.oschina.net/u/3113381/blog/5478108 1 JDK 相关的查找命令 (1)确认是否安装 JDK //命令 java -version //输出示例 ...
- ARM 平台Docker运行RabbitMQ 以及迁移的简单办法
公司网络很垃圾. 可以使用vps 进行下载和打包 放到 公司的机器上面进行使用. 1. 搜索有没有可用的镜像. [root@JNXLH ~]# docker search rabbitmq |gre ...
- 【K哥爬虫普法】北京某公司惨遭黑客攻击13000000余次,连夜报警……
我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K 哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识, ...