CodeQUEEN 2026 予選 (AtCoder Beginner Contest 462)
F
更多 ABC
More ABC
AtCoder
⌀ 1952
原题
← DG →

更多 ABCAI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a string $S$ consisting of uppercase English letters. Takahashi's goal is to increase the number of occurrences of ABC as a substring of $S$ by exactly $K$, by replacing some characters of $S$.

给定一个由大写英文字母组成的字符串 $S$。 高桥希望通过替换 $S$ 中的一些字符,使 ABC 作为子串在 $S$ 中出现的次数恰好增加 $K$ 次。AI (CI翻译)

Determine whether this is possible, and if so, find the minimum number of characters Takahashi needs to replace to achieve his goal.

请判断这是否可能;如果可能,求出为实现目标高桥至少需要替换的字符数。AI (CI翻译)

$T$ test cases are given; solve each one.

给出 $T$ 个测试用例,请分别求解。AI (CI翻译)

What does "replacing" mean? Replacing one character of $S$ means choosing an integer $i$ satisfying $1\leq i\leq \lvert S\rvert$ and replacing the $i$-th character of $S$ with any ** one ** uppercase English letter. Here, $\lvert S\rvert$ denotes the length of $S$.

“替换”是什么意思?替换 $S$ 中的一个字符,是指选择一个满足 $1\leq i\leq \lvert S\rvert$ 的整数 $i$,并将 $S$ 的第 $i$ 个字符替换为任意一个大写英文字母。其中,$\lvert S\rvert$ 表示字符串 $S$ 的长度。AI (CI翻译)

What is a substring? A substring of $S$ is a string obtained by removing zero or more characters from the beginning and end of $S$. Two substrings are counted as distinct if they are taken from different positions in $S$, even if they are equal as strings.

什么是子串?$S$ 的子串是通过从 $S$ 的开头和结尾删除零个或多个字符后得到的字符串。即使两个子串作为字符串相同,只要它们在 $S$ 中取自不同的位置,也会被视为不同的子串。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1\leq T\leq 10^5$
  • $S$ is a string of length between $3$ and $3\times 10^5$, inclusive, consisting of uppercase English letters.
  • $1\leq K \leq 10$
  • In each input, the total length of $S$ over all test cases is at most $3\times 10^5$.
  • $T$ and $K$ are integers.
  • $1\leq T\leq 10^5$
  • $S$ 是一个长度在 $3$ 至 $3\times 10^5$ 之间(包括端点)的字符串,仅由大写英文字母组成。
  • $1\leq K \leq 10$
  • 每组输入中,所有测试用例的 $S$ 的长度之和不超过 $3\times 10^5$。
  • $T$ 和 $K$ 均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入按以下格式给出:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

$\mathrm{case}_i$ represents the $i$-th test case. Each test case is given in the following format:

其中,$\mathrm{case}_i$ 表示第 $i$ 个测试用例。 每个测试用例按以下格式给出:AI (CI翻译)

S
K

S
K
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines. The $i$-th line $(1\leq i\leq T)$ should contain the answer for the $i$-th test case. For each test case, if it is impossible for Takahashi to achieve his goal, output $-1$; otherwise, output the minimum number of characters that need to be replaced to achieve his goal.

输出 $T$ 行。 第 $i$ 行 $(1\leq i\leq T)$ 应包含第 $i$ 个测试用例的答案。 对于每个测试用例,如果高桥无法实现目标,输出 $-1$;否则,输出为实现目标所需替换的最少字符数。AI (CI翻译)

样例

Input #1
5
ATCABC
1
ABABC
1
XABCYZ
1
ZZZZZ
1
ABCBABCAEFCABAABC
2
Output #1
1
-1
6
3
3
In the first test case, the given string $S=$`ATCABC` contains `ABC` as a substring once. Replacing the second character of $S$ with `B` gives $S=$`ABCABC`, which contains `ABC` as a substring twice. Thus, replacing one character increases the number of occurrences of `ABC` as a substring by $1$, achieving the goal. Therefore, output $1$ on the first line. In the second test case, the given string $S=$`ABABC` contains `ABC` as a substring once; no matter how the characters of $S$ are replaced, it is impossible to make $S$ contain `ABC` as a substring twice. Therefore, output $-1$ on the second line. In the third test case, the given string $S=$`XABCYZ` contains `ABC` as a substring once. To make $S$ contain `ABC` as a substring twice, all characters of $S$ must be replaced, giving $S=$`ABCABC`. Therefore, output $6$ on the third line.
题目信息
题号
abc462_f
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。