A14388 | Say No to Palindromes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let's call the string beautiful if it does not contain a substring of length at least $2$ , which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase).
You are given a string $s$ of length $n$ , each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase).
You have to answer $m$ queries — calculate the cost of the substring of the string $s$ from $l_i$ -th to $r_i$ -th position, inclusive.
Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase).
You are given a string $s$ of length $n$ , each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase).
You have to answer $m$ queries — calculate the cost of the substring of the string $s$ from $l_i$ -th to $r_i$ -th position, inclusive.
输入格式
The first line contains two integers $n$ and $m$ ( $1 \le n, m \le 2 \cdot 10^5$ ) — the length of the string $s$ and the number of queries.
The second line contains the string $s$ , it consists of $n$ characters, each character one of the first $3$ Latin letters.
The following $m$ lines contain two integers $l_i$ and $r_i$ ( $1 \le l_i \le r_i \le n$ ) — parameters of the $i$ -th query.
The second line contains the string $s$ , it consists of $n$ characters, each character one of the first $3$ Latin letters.
The following $m$ lines contain two integers $l_i$ and $r_i$ ( $1 \le l_i \le r_i \le n$ ) — parameters of the $i$ -th query.
输出格式
For each query, print a single integer — the cost of the substring of the string $s$ from $l_i$ -th to $r_i$ -th position, inclusive.
输入输出样例
输入 #1
5 4 baacb 1 3 1 5 4 5 2 3
输出 #1
1 2 0 1
Consider the queries of the example test.
- in the first query, the substring is baa, which can be changed to bac in one operation;
- in the second query, the substring is baacb, which can be changed to cbacb in two operations;
- in the third query, the substring is cb, which can be left unchanged;
- in the fourth query, the substring is aa, which can be changed to ba in one operation.
- in the first query, the substring is baa, which can be changed to bac in one operation;
- in the second query, the substring is baacb, which can be changed to cbacb in two operations;
- in the third query, the substring is cb, which can be left unchanged;
- in the fourth query, the substring is aa, which can be changed to ba in one operation.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted