CF1670B.Dorms War

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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 ss of length nn. He also knows that there are kk special letters of the alphabet: c1,c2,…,ckc_1,c_2,\ldots, c_k.

Hosssam made a program that can do the following.

  1. The program considers the current password ss of some length mm.
  2. Then it finds all positions ii (1≤i<m1\le i \lt m) such that si+1s_{i+1} is one of the kk special letters.
  3. Then it deletes all of those positions from the password ss even if sis_{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 ss is "abcdef" and the special characters are 'b' and 'd'. If he runs the program once, the positions 11 and 33 will be deleted as they come before special characters, so the password becomes "bdef". If he runs the program again, it deletes position 11, 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 决定在 Hemose 睡觉时偷偷潜入他的房间,修改他笔记本电脑的密码。他早已知道了当前密码,该密码是一个长度为 nn 的字符串 ss。他还知道字母表中有 kk 个特殊字母:c1,c2,…,ckc_1,c_2,\ldots, c_k。

Hosssam 编写了一个程序,能够执行以下操作:

  1. 程序考虑当前密码 ss(其长度为 mm);
  2. 然后找出所有满足 1≤i<m1\le i \lt m 的位置 ii,使得 si+1s_{i+1} 是 kk 个特殊字母之一;
  3. 接着,程序将这些位置 ii 上的字符全部从密码 ss 中删除(即使 sis_i 本身是特殊字符,也照删不误)。如果不存在任何可删除的位置,则程序会显示一条错误信息,该错误信息会发出非常响亮的声音。

例如,假设字符串 ss 为 "abcdef",特殊字符为 'b' 和 'd'。若运行一次该程序,则位置 11 和 33 将被删除(因为它们分别位于特殊字符 'b' 和 'd' 之前),因此密码变为 "bdef"。若再次运行该程序,则位置 11 被删除,密码变为 "def"。如果他足够明智,就不会再运行第三次。

Hosssam 想知道:他最多能对 Hemose 的笔记本电脑运行多少次该程序,而不会因错误信息的响声惊醒 Hemose?你能帮他解决这个问题吗?

输入格式

The first line contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases. Then tt test cases follow.

The first line of each test case contains a single integer nn (2≤n≤1052 \le n \le 10^5) — the initial length of the password.

The next line contains a string ss consisting of nn lowercase English letters — the initial password.

The next line contains an integer kk (1≤k≤261 \le k \le 26), followed by kk distinct lowercase letters c1,c2,…,ckc_1,c_2,\ldots,c_k — the special letters.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5)—— 测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 初始密码的长度。

下一行包含一个由 nn 个小写英文字母组成的字符串 ss —— 初始密码。

再下一行包含一个整数 kk(1≤k≤261 \le k \le 26),后跟 kk 个互不相同的小写字母 c1,c2,…,ckc_1,c_2,\ldots,c_k —— 特殊字母。

保证所有测试用例的 nn 之和不超过 2⋅1052\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.

对于每个测试用例,在一行中输出 Hosssam 在不显示错误信息的情况下最多能运行该程序的次数。

输入输出样例

  • 输入#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 55 times as follows: iloveslim→ilovslim→iloslim→ilslim→islim→slim\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 22 times as follows: joobeel→oel→el\text{joobeel} \to \text{oel} \to \text{el}

In the third test case, the program can run 33 times as follows: basiozi→bioi→ii→i\text{basiozi} \to \text{bioi} \to \text{ii} \to \text{i}.

In the fourth test case, the program can run 55 times as follows: khater→khatr→khar→khr→kr→r\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: abobeih→h\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.

在第一个测试用例中,程序可以运行 55 次,过程如下:iloveslim→ilovslim→iloslim→ilslim→islim→slim\text{iloveslim} \to \text{ilovslim} \to \text{iloslim} \to \text{ilslim} \to \text{islim} \to \text{slim}

在第二个测试用例中,程序可以运行 22 次,过程如下:joobeel→oel→el\text{joobeel} \to \text{oel} \to \text{el}

在第三个测试用例中,程序可以运行 33 次,过程如下:basiozi→bioi→ii→i\text{basiozi} \to \text{bioi} \to \text{ii} \to \text{i}

在第四个测试用例中,程序可以运行 55 次,过程如下:khater→khatr→khar→khr→kr→r\text{khater} \to \text{khatr} \to \text{khar} \to \text{khr} \to \text{kr} \to \text{r}

在第五个测试用例中,程序只能运行 11 次,过程如下:abobeih→h\text{abobeih} \to \text{h}

在第六个测试用例中,程序无法运行,因为密码中没有任何字符是特殊字符。

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

首页