Content

给定一个长度为 \(n\) 且仅包含字符 ab 的字符串 \(s\)。请找出任意一个使得 ab 数量相等的 \(s\) 的子串并输出其起始位置和终止位置。如果不存在请输出 -1 -1

数据范围:\(t\) 组数据,\(1\leqslant t\leqslant 1000\),\(1\leqslant n\leqslant 50\)。

Solution

不难想到一种很朴素的做法:直接暴力提取出所有 \(s\) 的子串,然后一个一个判断这些子串里头是否有 ab 数量相等的子串。这样做的理论复杂度是 \(\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 题解的更多相关文章

  1. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

  2. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  3. Codeforces 873 B. Balanced Substring(前缀和 思维)

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  4. 【Cf edu 30 B. Balanced Substring】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. 837B. Balanced Substring

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Balanced Substring

    You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...

  7. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  8. 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 ...

  9. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

随机推荐

  1. [SVN] Branch and Tag

    在 SVN 中,如何建立分支以及如何标记Tag. 右键要处理的文件夹,选择 "TortoiseSVN" - "Branch/tag...",进入下面界面: To ...

  2. Java 代码审计 — 1. ClassLoader

    参考: https://www.bilibili.com/video/BV1go4y197cL/ https://www.baeldung.com/java-classloaders https:// ...

  3. CF1562E Rescue Niwen!

    开始的时候只会一个\(O(n^2log)\) 即做出所有的\(n^2\)串,显然可以用\(SAM\)来进行这样一个排序,然后\(log\)做. 但这种题我们显然要找一些友好的性质: 我们发现字符串的比 ...

  4. Atcoder Grand Contest 038 F - Two Permutations(集合划分模型+最小割)

    洛谷题面传送门 & Atcoder 题面传送门 好久前做的题了--今天偶然想起来要补个题解 首先考虑排列 \(A_i\) 要么等于 \(i\),要么等于 \(P_i\) 这个条件有什么用.我们 ...

  5. Python list的深拷贝和浅拷贝

    深拷贝和浅拷贝 列表存储数据,列表拷贝就是数据备份 浅拷贝 优点:占用内存较少 缺点:修改深层数据,会影响原数据 深拷贝 优点:修改数据,互不影响 缺点:占用内存较大 ""&quo ...

  6. MISA(在线)注释叶绿体基因组SSR

    SSR (Simple Sequence Repeat),即简单重复序列,是一种以PCR技术为核心的DNA分子标记技术,也称为微卫星序列或者串联重复. 简单重复顾名思义就是以很短的序列为一个单元,比如 ...

  7. 一个画组织解剖图R包

    地址: https://github.com/jespermaag/gganatogram

  8. mysql order by 多个字段排序实现组内排序

    总结:大组在前,小组在后,计量值再最后,即可实现组内排序:下边是参考别人的具体实例: 工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问 ...

  9. PHP识别二维码(php-zbarcode)

    PHP识别二维码(php-zbarcode) 标签: php二维码扩展 2015-11-06 17:12 609人阅读 评论(0) 收藏 举报  分类: PHP(1)  Linux 版权声明:本文为博 ...

  10. kubernetes部署haproxy、keepalived为kube-apiserver做集群

    也可以用nginx.keepalived做负载均衡,看大家的需求. # yum -y install haproxy keepalived haproxy的配置文件(三台一样): cat > / ...