A15013 | Dorms War
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Hosssam decided to sneak into Hemose's room while he is sleeping and change his laptop's password. He already knows the password, which is a string $s$ of length $n$ . He also knows that there are $k$ special letters of the alphabet: $c_1,c_2,\ldots, c_k$ .
Hosssam made a program that can do the following.
1. The program considers the current password $s$ of some length $m$ .
2. Then it finds all positions $i$ ( $1\le i<m$ ) such that $s_{i+1}$ is one of the $k$ special letters.
3. Then it deletes all of those positions from the password $s$ even if $s_{i}$ is a special character. If there are no positions to delete, then the program displays an error message which has a very loud sound.
For example, suppose the string $s$ is "abcdef" and the special characters are 'b' and 'd'. If he runs the program once, the positions $1$ and $3$ will be deleted as they come before special characters, so the password becomes "bdef". If he runs the program again, it deletes position $1$ , and the password becomes "def". If he is wise, he won't run it a third time.
Hosssam wants to know how many times he can run the program on Hemose's laptop without waking him up from the sound of the error message. Can you help him?
Hosssam made a program that can do the following.
1. The program considers the current password $s$ of some length $m$ .
2. Then it finds all positions $i$ ( $1\le i<m$ ) such that $s_{i+1}$ is one of the $k$ special letters.
3. Then it deletes all of those positions from the password $s$ even if $s_{i}$ is a special character. If there are no positions to delete, then the program displays an error message which has a very loud sound.
For example, suppose the string $s$ is "abcdef" and the special characters are 'b' and 'd'. If he runs the program once, the positions $1$ and $3$ will be deleted as they come before special characters, so the password becomes "bdef". If he runs the program again, it deletes position $1$ , and the password becomes "def". If he is wise, he won't run it a third time.
Hosssam wants to know how many times he can run the program on Hemose's laptop without waking him up from the sound of the error message. Can you help him?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^5$ ) — the number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ( $2 \le n \le 10^5$ ) — the initial length of the password.
The next line contains a string $s$ consisting of $n$ lowercase English letters — the initial password.
The next line contains an integer $k$ ( $1 \le k \le 26$ ), followed by $k$ distinct lowercase letters $c_1,c_2,\ldots,c_k$ — the special letters.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $2 \le n \le 10^5$ ) — the initial length of the password.
The next line contains a string $s$ consisting of $n$ lowercase English letters — the initial password.
The next line contains an integer $k$ ( $1 \le k \le 26$ ), followed by $k$ distinct lowercase letters $c_1,c_2,\ldots,c_k$ — the special letters.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$ .
输出格式
For each test case, print the maximum number of times Hosssam can run the program without displaying the error message, on a new line.
输入输出样例
输入 #1
10 9 iloveslim 1 s 7 joobeel 2 o e 7 basiozi 2 s i 6 khater 1 r 7 abobeih 6 a b e h i o 5 zondl 5 a b c e f 6 shoman 2 a h 7 shetwey 2 h y 5 samez 1 m 6 mouraz 1 m
输出 #1
5 2 3 5 1 0 3 5 2 0
In the first test case, the program can run $5$ times as follows: $\text{iloveslim} \to \text{ilovslim} \to \text{iloslim} \to \text{ilslim} \to \text{islim} \to \text{slim}$
In the second test case, the program can run $2$ times as follows: $\text{joobeel} \to \text{oel} \to \text{el}$
In the third test case, the program can run $3$ times as follows: $\text{basiozi} \to \text{bioi} \to \text{ii} \to \text{i}$ .
In the fourth test case, the program can run $5$ times as follows: $\text{khater} \to \text{khatr} \to \text{khar} \to \text{khr} \to \text{kr} \to \text{r}$
In the fifth test case, the program can run only once as follows: $\text{abobeih} \to \text{h}$
In the sixth test case, the program cannot run as none of the characters in the password is a special character.
In the second test case, the program can run $2$ times as follows: $\text{joobeel} \to \text{oel} \to \text{el}$
In the third test case, the program can run $3$ times as follows: $\text{basiozi} \to \text{bioi} \to \text{ii} \to \text{i}$ .
In the fourth test case, the program can run $5$ times as follows: $\text{khater} \to \text{khatr} \to \text{khar} \to \text{khr} \to \text{kr} \to \text{r}$
In the fifth test case, the program can run only once as follows: $\text{abobeih} \to \text{h}$
In the sixth test case, the program cannot run as none of the characters in the password is a special character.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted