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 ...
随机推荐
- Vue: 一个简单的Vue2.0 v-model双向数据绑定的实现,含源代码,小白也能看懂
首先说一下原理吧 View层(dom元素)的变动如何响应到Model层(Js变量)呢? 通过监听元素的input事件来动态的改变js变量的值,实际上不是改变的js变量的值,而是改变的js变量的gett ...
- maven项目打包不带版本号的操作
- ppt的作用
ppt不重要,使用ppt的场合很重要. 演讲分享,答辩总结,商业竞标,新年计划,年终总结.
- HTML四种定位-粘滞定位
粘滞定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...
- 【Python小试】根据外显子位置生成CDS序列
已知 genomic_dna.txt TCGATCGTACCGTCGACGATGCTACGATCGTCGATCGTAGTCGATCATCGATCGATCGACTGATCGATCGATCGATCGATC ...
- 重学Git(一)
一.最最最基础操作 # 初始化仓库 git init # 添加文件到暂存区 git add readme.md # 提交 git commit -m 'wrote a readme file' 二.简 ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- 零基础学习java------day5------do....while循环、嵌套、方法(函数)
1 do...while循环 格式 初始化语句; do { 循环体语句; 控制条件语句; }while(判断条件语句); 流程: 先执行初始化语句 再执行循环体语句 再执行条件控制语句 再做条件的判 ...
- 移动开发之h5学习大纲
移动开发学习形式:授课.自学 1.html5 css3 htm5shiv.js response.js 2.流式布局 自适应布局 盒模型 弹性盒模型 响应式布局3.iscroll swiper boo ...
- 2016广东工业大学新生杯决赛 A-pigofzhou的巧克力棒
题目:GDUTOJ | pigofzhou的巧克力棒 (gdutcode.cn) 之前看了大佬博客的题解,一直没懂(我太菜了),后来听了朋友@77的讲解,终于懂了. 和拆分出2的n次方不一样,这是一种 ...