CF1555D.Say No to Palindromes
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 li-th to ri-th position, inclusive.
我们称一个字符串是“优美的”,如果它不包含长度至少为 2 的回文子串。回忆一下,回文串是指从第一个字符读到最后一个字符,与从最后一个字符读到第一个字符完全相同的字符串。例如,字符串 a、bab、acca、bcabcbacb 是回文串,但字符串 ab、abbbaa、cccb 不是。
我们定义一个字符串的“代价”为:使其变为优美字符串所需的最少操作次数;每次操作允许将字符串中的任意一个字符修改为拉丁字母表前 3 个小写字母(即 a、b、c)中的任意一个。
给定一个长度为 n 的字符串 s,其中每个字符均为拉丁字母表前 3 个小写字母之一(即 a、b、c)。
你需要回答 m 个查询:对第 i 个查询,计算字符串 s 中从第 li 个位置到第 ri 个位置(含端点)的子串的代价。
输入格式
The first line contains two integers n and m (1≤n,m≤2⋅105) — 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 li and ri (1≤li≤ri≤n) — parameters of the i-th query.
第一行包含两个整数 n 和 m(1≤n,m≤2⋅105)—— 分别表示字符串 s 的长度和查询次数。
第二行包含字符串 s,其由 n 个字符组成,每个字符均为前 3 个拉丁字母之一。
接下来的 m 行每行包含两个整数 li 和 ri(1≤li≤ri≤n)—— 表示第 i 个查询的参数。
输出格式
For each query, print a single integer — the cost of the substring of the string s from li-th to ri-th position, inclusive.
对于每个查询,输出一个整数——字符串 s 中从第 li 个位置到第 ri 个位置(包含两端)的子串的代价。
输入输出样例
输入#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.
考虑示例测试中的查询。
- 在第一个查询中,子字符串为
baa,可通过一次操作将其变为bac; - 在第二个查询中,子字符串为
baacb,可通过两次操作将其变为cbacb; - 在第三个查询中,子字符串为
cb,可保持不变; - 在第四个查询中,子字符串为
aa,可通过一次操作将其变为ba。
输入解题思路,AI测评打分。不知道怎么写?