B. Petya and Exam

Codeforces
IDCF832B
Time2000ms
Memory256MB
Difficulty
implementationstrings
English · Original
Chinese · Translation
Formal · Original
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one.. There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "_?_" and "_*_"). It is known that character "_*_" occurs **no more than once** in the pattern. Also, _n_ query strings are given, it is required to determine for each of them if the pattern matches it or not. Everything seemed easy to Petya, but then he discovered that **the special pattern characters differ from their usual meaning**. A pattern matches a string if it is possible to replace each character "_?_" with one _good_ lowercase English letter, and the character "_*_" (if there is one) with any, including empty, string of _bad_ lowercase English letters, so that the resulting string is the same as the given string. The good letters are given to Petya. All the others are bad. ## Input The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad. The second line contains the pattern — a string _s_ of lowercase English letters, characters "_?_" and "_*_" (1 ≤ |_s_| ≤ 105). It is guaranteed that character "_*_" occurs in _s_ no more than once. The third line contains integer _n_ (1 ≤ _n_ ≤ 105) — the number of query strings. _n_ lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string. It is guaranteed that the total length of all query strings is not greater than 105. ## Output Print _n_ lines: in the _i_\-th of them print "_YES_" if the pattern matches the _i_\-th query string, and "_NO_" otherwise. You can choose the case (lower or upper) for each letter arbitrary. [samples] ## Note In the first example we can replace "_?_" with good letters "_a_" and "_b_", so we can see that the answer for the first query is "_YES_", and the answer for the second query is "_NO_", because we can't match the third letter. Explanation of the second example. * The first query: "_NO_", because character "_*_" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "_ba_", in which both letters are good. * The second query: "_YES_", because characters "_?_" can be replaced with corresponding good letters, and character "_*_" can be replaced with empty string, and the strings will coincide. * The third query: "_NO_", because characters "_?_" can't be replaced with bad letters. * The fourth query: "_YES_", because characters "_?_" can be replaced with good letters "_a_", and character "_*_" can be replaced with a string of bad letters "_x_".
现在是艰难时刻。今天 Petya 需要在信息学考试中获得 100 分。题目对 Petya 来说似乎很简单,但他认为自己没有足够时间完成所有题目,因此他请你帮忙解决其中一个。 题面中有一个通配符模式(由小写英文字母、字符 "_?_" 和 "_*_" 组成的字符串)。已知字符 "_*_" 在模式中最多出现一次。 此外,给出了 #cf_span[n] 个查询字符串,需要判断每个字符串是否与该模式匹配。 Petya 原本觉得一切都很简单,但他后来发现,这些特殊模式字符的含义与通常不同。 当满足以下条件时,模式与字符串匹配:可以将每个字符 "_?_" 替换为一个 _好_ 的小写英文字母,将字符 "_*_"(如果存在)替换为任意长度(包括空)的 _坏_ 小写英文字母组成的字符串,使得得到的字符串与给定字符串完全相同。 好字母由 Petya 给出,其余所有字母均为坏字母。 第一行包含一个长度为 #cf_span[1] 到 #cf_span[26] 的字符串,由互不相同的英文小写字母组成。这些字母是好字母,其余均为坏字母。 第二行包含模式 —— 一个由小写英文字母、字符 "_?_" 和 "_*_" 组成的字符串 #cf_span[s](#cf_span[1 ≤ |s| ≤ 105])。保证字符 "_*_" 在 #cf_span[s] 中出现次数不超过一次。 第三行包含整数 #cf_span[n](#cf_span[1 ≤ n ≤ 105])—— 查询字符串的数量。 接下来 #cf_span[n] 行,每行包含一个非空的、由小写英文字母组成的字符串 —— 一个查询字符串。 保证所有查询字符串的总长度不超过 #cf_span[105]。 请输出 #cf_span[n] 行:在第 #cf_span[i] 行,如果模式匹配第 #cf_span[i] 个查询字符串,则输出 "_YES_",否则输出 "_NO_"。 你可以任意选择每个字母的大小写形式。 在第一个例子中,我们可以将 "_?_" 替换为好字母 "_a_" 和 "_b_",因此第一个查询的答案是 "_YES_",第二个查询的答案是 "_NO_",因为我们无法匹配第三个字母。 第二个例子的解释: ## Input 第一行包含一个长度为 #cf_span[1] 到 #cf_span[26] 的字符串,由互不相同的英文小写字母组成。这些字母是好字母,其余均为坏字母。第二行包含模式 —— 一个由小写英文字母、字符 "_?_" 和 "_*_" 组成的字符串 #cf_span[s](#cf_span[1 ≤ |s| ≤ 105])。保证字符 "_*_" 在 #cf_span[s] 中出现次数不超过一次。第三行包含整数 #cf_span[n](#cf_span[1 ≤ n ≤ 105])—— 查询字符串的数量。#cf_span[n] 行接下来,每行包含一个非空的、由小写英文字母组成的字符串 —— 一个查询字符串。保证所有查询字符串的总长度不超过 #cf_span[105]。 ## Output 输出 #cf_span[n] 行:在第 #cf_span[i] 行,如果模式匹配第 #cf_span[i] 个查询字符串,则输出 "_YES_",否则输出 "_NO_"。你可以任意选择每个字母的大小写形式。 [samples] ## Note 在第一个例子中,我们可以将 "_?_" 替换为好字母 "_a_" 和 "_b_",因此第一个查询的答案是 "_YES_",第二个查询的答案是 "_NO_",因为我们无法匹配第三个字母。 第二个例子的解释: 第一个查询:"_NO_",因为字符 "_*_" 只能被替换为坏字母组成的字符串,但要匹配查询字符串,必须将其替换为字符串 "_ba_",而其中两个字母都是好字母。 第二个查询:"_YES_",因为 "_?_" 可以被替换为对应的好的字母,而 "_*_" 可以被替换为空字符串,从而使两个字符串完全一致。 第三个查询:"_NO_",因为 "_?_" 不能被替换为坏字母。 第四个查询:"_YES_",因为 "_?_" 可以被替换为好字母 "_a_",而 "_*_" 可以被替换为坏字母组成的字符串 "_x_"。
**Definitions** Let $ G \subseteq \Sigma $ be the set of *good* lowercase English letters, given as a string of distinct characters. Let $ B = \Sigma \setminus G $ be the set of *bad* lowercase English letters. Let $ p \in (\Sigma \cup \{?, *\})^* $ be the *pattern*, with $ |p| \leq 10^5 $, and containing at most one `*` character. Let $ Q = \{q_1, q_2, \dots, q_n\} $ be a set of $ n \leq 10^5 $ query strings, each $ q_i \in \Sigma^+ $, and $ \sum_{i=1}^n |q_i| \leq 10^5 $. **Constraints** 1. $ 1 \leq |G| \leq 26 $, $ G $ consists of distinct lowercase English letters. 2. $ p $ contains only lowercase English letters, `?`, and at most one `*`. 3. Each query string $ q_i $ is non-empty and consists only of lowercase English letters. 4. Total length of all query strings $ \leq 10^5 $. **Objective** For each query string $ q \in Q $, determine whether there exists a string $ q' $ such that: - $ q' = q $, - and $ q' $ can be obtained from $ p $ by replacing: - each `?` with *exactly one* character from $ G $, - the `*` (if present) with *any* (possibly empty) string composed *only* of characters from $ B $, - all other characters in $ p $ remain unchanged. Output "YES" if such a replacement exists for $ q $, otherwise "NO".
Samples
Input #1
ab
a?a
2
aaa
aab
Output #1
YES
NO
Input #2
abc
a?a?a*
4
abacaba
abaca
apapa
aaaaax
Output #2
NO
YES
NO
YES
API Response (JSON)
{
  "problem": {
    "name": "B. Petya and Exam",
    "description": {
      "content": "It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one.. There i",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF832B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..\n\nThere i...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "现在是艰难时刻。今天 Petya 需要在信息学考试中获得 100 分。题目对 Petya 来说似乎很简单,但他认为自己没有足够时间完成所有题目,因此他请你帮忙解决其中一个。\n\n题面中有一个通配符模式(由小写英文字母、字符 \"_?_\" 和 \"_*_\" 组成的字符串)。已知字符 \"_*_\" 在模式中最多出现一次。\n\n此外,给出了 #cf_span[n] 个查询字符串,需要判断每个字符串是否与该模式匹配...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G \\subseteq \\Sigma $ be the set of *good* lowercase English letters, given as a string of distinct characters.  \nLet $ B = \\Sigma \\setminus G $ be the set of *bad* lowercase En...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments