AT_abc462_f.More ABC

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

TT test cases are given; solve each one.

What does "replacing" mean?

Replacing one character of SS means choosing an integer ii satisfying 1≤i≤∣S∣1\leq i\leq \lvert S\rvert and replacing the ii-th character of SS with any one uppercase English letter.
Here, ∣S∣\lvert S\rvert denotes the length of SS.

What is a substring?

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

给你一个由大写英文字母组成的字符串 SS。
高桥的目标是通过替换 SS 中的某些字符,恰好增加 SS 中子串 ABC 的出现次数 KK 次。

请判断该目标是否可行;若可行,求出高桥为达成目标所需替换的最少字符数。

共给出 TT 组测试数据,请对每组数据求解。

什么是“替换”?

替换 SS 中的一个字符,是指选择一个满足 1≤i≤∣S∣1\leq i\leq \lvert S\rvert 的整数 ii,并将 SS 的第 ii 个字符替换为任意一个大写英文字母。
其中,∣S∣\lvert S\rvert 表示字符串 SS 的长度。

什么是子串?

字符串 SS 的一个子串,是指从 SS 的开头和结尾分别删去零个或多个字符后所得的字符串。
即使两个子串作为字符串相等,只要它们在 SS 中起始位置不同,就视为不同的子串。

输入格式

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

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

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

SS
KK

输入从标准输入给出,格式如下:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

casei\mathrm{case}_i 表示第 ii 个测试用例。
每个测试用例的格式如下:

SS
KK

输出格式

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

输出 TT 行。
第 ii 行(1≤i≤T1\leq i\leq T)应包含第 ii 个测试用例的答案。
对于每个测试用例,若高桥无法实现其目标,则输出 −1-1;否则,输出为实现其目标所需替换的最少字符数。

输入输出样例

  • 输入#1

    5
    ATCABC
    1
    ABABC
    1
    XABCYZ
    1
    ZZZZZ
    1
    ABCBABCAEFCABAABC
    2

    输出#1

    1
    -1
    6
    3
    3

说明/提示

Sample 1 Explanation:
In the first test case, the given string S=S=ATCABC contains ABC as a substring once.
Replacing the second character of SS with B gives S=S=ABCABC, which contains ABC as a substring twice.
Thus, replacing one character increases the number of occurrences of ABC as a substring by 11, achieving the goal. Therefore, output 11 on the first line.

In the second test case, the given string S=S=ABABC contains ABC as a substring once; no matter how the characters of SS are replaced, it is impossible to make SS contain ABC as a substring twice.
Therefore, output −1-1 on the second line.

In the third test case, the given string S=S=XABCYZ contains ABC as a substring once.
To make SS contain ABC as a substring twice, all characters of SS must be replaced, giving S=S=ABCABC.
Therefore, output 66 on the third line.

Constraints

  • 1≤T≤1051\leq T\leq 10^5
  • SS is a string of length between 33 and 3×1053\times 10^5, inclusive, consisting of uppercase English letters.
  • 1≤K≤101\leq K \leq 10
  • In each input, the total length of SS over all test cases is at most 3×1053\times 10^5.
  • TT and KK are integers.

样例 1 解释:
在第一个测试用例中,给定字符串 S=S=ATCABC 包含子串 ABC 恰好一次。
将 SS 的第二个字符替换为 B,得到 S=S=ABCABC,该字符串包含子串 ABC 两次。
因此,仅需替换一个字符,即可使子串 ABC 的出现次数增加 11,从而达成目标。故第一行输出 11。

在第二个测试用例中,给定字符串 S=S=ABABC 包含子串 ABC 恰好一次;无论怎样替换 SS 中的字符,都不可能使 SS 包含子串 ABC 两次。
因此,第二行输出 −1-1。

在第三个测试用例中,给定字符串 S=S=XABCYZ 包含子串 ABC 恰好一次。
为使 SS 包含子串 ABC 两次,必须替换 SS 的所有字符,得到 S=S=ABCABC。
因此,第三行输出 66。

约束条件

  • 1≤T≤1051\leq T\leq 10^5
  • SS 是一个长度在 33 到 3×1053\times 10^5(含)之间的字符串,仅由大写英文字母组成。
  • 1≤K≤101\leq K \leq 10
  • 在每组输入中,所有测试用例的 SS 的总长度不超过 3×1053\times 10^5。
  • TT 和 KK 均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页