103220: [Atcoder]ABC322 A - First ABC 2
Description
Score : $100$ points
Problem Statement
You are given a string $S$ of length $N$ consisting of A
, B
, and C
.
Find the position where ABC
first appears as a (contiguous) substring in $S$. In other words, find the smallest integer $n$ that satisfies all of the following conditions.
- $1 \leq n \leq N - 2$.
- The string obtained by extracting the $n$-th through $(n+2)$-th characters of $S$ is
ABC
.
If ABC
does not appear in $S$, print -1
.
Constraints
- $3 \leq N \leq 100$
- $S$ is a string of length $N$ consisting of
A
,B
, andC
.
Input
The input is given from Standard Input in the following format:
$N$ $S$
Output
Print the position where ABC
first appears as a substring in $S$, or -1
if it does not appear in $S$.
Sample Input 1
8 ABABCABC
Sample Output 1
3
ABC
first appears in $S$ at the $3$-rd through $5$-th characters of $S$. Therefore, the answer is $3$.
Sample Input 2
3 ACB
Sample Output 2
-1
If ABC
does not appear in $S$, print $-1$.
Sample Input 3
20 BBAAABBACAACABCBABAB
Sample Output 3
13
Output
问题描述
你将得到一个长度为 N 的字符串 S,由 A
、B
和 C
组成。
找到 ABC
第一次作为(连续)子字符串出现在 S 中的位置。换句话说,找到满足以下所有条件的最小整数 n。
- $1 \leq n \leq N - 2$。
- 提取 S 的第 n 个到第 $(n+2)$ 个字符得到的字符串为
ABC
。
如果 ABC
不在 S 中出现,打印 -1
。
约束条件
- $3 \leq N \leq 100$
- S 是一个长度为 N 的字符串,由
A
、B
和C
组成。
输入
输入通过标准输入以以下格式给出:
$N$ $S$
输出
打印 ABC
第一次作为子字符串出现在 S 中的位置,如果它不在 S 中出现,则打印 -1
。
样例输入 1
8 ABABCABC
样例输出 1
3
ABC
第一次出现在 S 中的位置为 S 的第 3 个到第 5 个字符。因此,答案是 3。
样例输入 2
3 ACB
样例输出 2
-1
如果 ABC
不在 S 中出现,打印 -1
。
样例输入 3
20 BBAAABBACAACABCBABAB
样例输出 3
13