CF1569A Balanced Substring 题解
Content
给定一个长度为 \(n\) 且仅包含字符 a
、b
的字符串 \(s\)。请找出任意一个使得 a
、b
数量相等的 \(s\) 的子串并输出其起始位置和终止位置。如果不存在请输出 -1 -1
。
数据范围:\(t\) 组数据,\(1\leqslant t\leqslant 1000\),\(1\leqslant n\leqslant 50\)。
Solution
不难想到一种很朴素的做法:直接暴力提取出所有 \(s\) 的子串,然后一个一个判断这些子串里头是否有 a
、b
数量相等的子串。这样做的理论复杂度是 \(\mathcal O(n^3)\),足够通过此题,更何况实际远远达不到这个复杂度。
但是!这里有一种 \(\mathcal O(n)\) 的做法,可以轻松通过此题!
不难想到,如果这个字符串里面既有 a
又有 b
的话,一定可以找到形如 ab
或者 ba
的字符串。于是我们直接扫一遍字符串,看是否有 \(s[i;i+1]\) 为 ab
或者 ba
,那么就可以直接输出 \(i\) 和 \(i+1\) 了。当然要特判无解的情况。
Code
namespace Solution {
string s;
iv Main() {
MT {
int n; read(n), cin >> s;
if((s.find("a") == string :: npos) ^ (s.find("b") == string :: npos)) {puts("-1 -1"); continue;}
F(int, i, 0, n - 1) if(s[i] != s[i + 1]) {printf("%d %d\n", i + 1, i + 2); break;}
}
}
}
CF1569A Balanced Substring 题解的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- codefroces 873 B. Balanced Substring && X73(前缀和思想)
B. Balanced Substring You are given a string s consisting only of characters 0 and 1. A substring [l ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
随机推荐
- 【SpringBoot】(1)-- 基于eclipse配置springboot开发环境
基于eclipse配置springboot开发环境 1. 下载并配置eclipse ① 前往eclipse官网 https://www.eclipse.org/downloads/packages/ ...
- java8两个字段进行排序问题
//这个解决问题 Comparator<Anjianxinxi> getLianriqi = Comparator.comparing(Anjianxinxi::getLianriqi). ...
- IPv6 寻址方式简介
在计算机网络中,寻址模式是指在网络上托管地址的机制.IPv6 提供了多种类型的模式,可以通过这些模式对单个主机进行寻址.也可以同时对多个主机进行寻址或者寻址最近距离的主机. 单播寻址 在单播寻址方式 ...
- 7.4 k8s结合ceph rbd、cephfs实现数据的持久化和共享
1.在ceph集群中创建rbd存储池.镜像及普通用户 1.1.存储池接镜像配置 创建存储池 root@u20-deploy:~# ceph osd pool create rbd-test-pool1 ...
- Codeforces 1408I - Bitwise Magic(找性质+集合幂级数)
Codeforces 题面传送门 & 洛谷题面传送门 Yet another immortal D1+D2 I %%%%%% 首先直接统计肯定是非常不容易的,不过注意到这个 \(k\) 非常小 ...
- dlang 读取gz压缩文件
没找到打开gz压缩文件的标准库,暂时调用系统命令打开gz压缩文件(参考:https://dlang.org/phobos/std_process.html#.Redirect.stdoutToStde ...
- 42-Remove Nth Node From End of List
Remove Nth Node From End of List My Submissions QuestionEditorial Solution Total Accepted: 106592 To ...
- sqlalchemy模块的基本使用
Python中SQLAlchemy模块通过建立orm来对数据库进行操作 1. 建表 方式1 # -*- coding:utf-8 -*- # Author:Wong Du from sqlalchem ...
- 打造基于 PostgreSQL/openGauss 的分布式数据库解决方案
在 MySQL ShardingSphere-Proxy 逐渐成熟并被广泛采用的同时,ShardingSphere 团队也在 PostgreSQL ShardingSphere-Proxy 上持续发力 ...
- Git分布式版本控制系统基础
查看创建的账号 下来在该当前的⽬录下创建⽂件,并且进⾏提交 使⽤git log就可以看到最近提交的⽇志记录的信息 查看窗户的状态信息 某些时候我们可能需要回退到之前的版本,那么具体处理的步骤为: 1. ...