A. Single Wildcard Pattern Matching

Codeforces
IDCF1023A
Time1000ms
Memory256MB
Difficulty
brute forceimplementationstrings
English · Original
Chinese · Translation
Formal · Original
You are given two strings $s$ and $t$. The string $s$ consists of lowercase Latin letters and **at most one** wildcard character '_*_', the string $t$ consists only of lowercase Latin letters. The length of the string $s$ equals $n$, the length of the string $t$ equals $m$. The wildcard character '_*_' in the string $s$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $s$ can be replaced with anything. If it is possible to replace a wildcard character '_*_' in $s$ to obtain a string $t$, then the string $t$ matches the pattern $s$. For example, if $s=$"_aba*aba_" then the following strings match it "_abaaba_", "_abacaba_" and "_abazzzaba_", but the following strings do not match: "_ababa_", "_abcaaba_", "_codeforces_", "_aba1aba_", "_aba?aba_". If the given string $t$ matches the given string $s$, print "_YES_", otherwise print "_NO_". ## Input The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the string $t$, respectively. The second line contains string $s$ of length $n$, which consists of lowercase Latin letters and **at most one** wildcard character '_*_'. The third line contains string $t$ of length $m$, which consists only of lowercase Latin letters. ## Output Print "_YES_" (without quotes), if you can obtain the string $t$ from the string $s$. Otherwise print "_NO_" (without quotes). [samples] ## Note In the first example a wildcard character '_*_' can be replaced with a string "_force_". So the string $s$ after this replacement is "_codeforces_" and the answer is "_YES_". In the second example a wildcard character '_*_' can be replaced with an empty string. So the string $s$ after this replacement is "_vkcup_" and the answer is "_YES_". There is no wildcard character '_*_' in the third example and the strings "_v_" and "_k_" are different so the answer is "_NO_". In the fourth example there is no such replacement of a wildcard character '_*_' that you can obtain the string $t$ so the answer is "_NO_".
你被给定两个字符串 $s$ 和 $t$。字符串 $s$ 由小写拉丁字母和至多一个通配符 '_*_' 组成,字符串 $t$ 仅由小写拉丁字母组成。字符串 $s$ 的长度为 $n$,字符串 $t$ 的长度为 $m$。 字符串 $s$ 中的通配符 '_*_'(如果存在)可以被任意长度(包括空)的小写拉丁字母序列替换。$s$ 中的其他字符不能被替换。如果可以通过替换 $s$ 中的通配符 '_*_' 得到字符串 $t$,则称字符串 $t$ 匹配模式 $s$。 例如,若 $s =$"_aba*aba_",则以下字符串与之匹配:"_abaaba_"、"_abacaba_" 和 "_abazzzaba_",但以下字符串不匹配:"_ababa_"、"_abcaaba_"、"_codeforces_"、"_aba1aba_"、"_aba?aba_"。 如果给定的字符串 $t$ 匹配给定的字符串 $s$,请输出 "_YES_",否则输出 "_NO_"。 第一行包含两个整数 $n$ 和 $m$ ($1 lt.eq n, m lt.eq 2 dot.op 10^5$) —— 分别表示字符串 $s$ 和 $t$ 的长度。 第二行包含长度为 $n$ 的字符串 $s$,由小写拉丁字母和至多一个通配符 '_*_' 组成。 第三行包含长度为 $m$ 的字符串 $t$,仅由小写拉丁字母组成。 如果你能从字符串 $s$ 得到字符串 $t$,请输出 "_YES_"(不含引号),否则输出 "_NO_"(不含引号)。 在第一个例子中,通配符 '_*_' 可以被字符串 "_force_" 替换。因此替换后的字符串 $s$ 为 "_codeforces_",答案为 "_YES_"。 在第二个例子中,通配符 '_*_' 可以被空字符串替换。因此替换后的字符串 $s$ 为 "_vkcup_",答案为 "_YES_"。 在第三个例子中,字符串中没有通配符 '_*_',且字符串 "_v_" 和 "_k_" 不同,因此答案为 "_NO_"。 在第四个例子中,不存在任何对通配符 '_*_' 的替换方式能获得字符串 $t$,因此答案为 "_NO_"。 ## Input 第一行包含两个整数 $n$ 和 $m$ ($1 lt.eq n, m lt.eq 2 dot.op 10^5$) —— 分别表示字符串 $s$ 和 $t$ 的长度。第二行包含长度为 $n$ 的字符串 $s$,由小写拉丁字母和至多一个通配符 '_*_' 组成。第三行包含长度为 $m$ 的字符串 $t$,仅由小写拉丁字母组成。 ## Output 如果你能从字符串 $s$ 得到字符串 $t$,请输出 "_YES_"(不含引号),否则输出 "_NO_"(不含引号)。 [samples] ## Note 在第一个例子中,通配符 '_*_' 可以被字符串 "_force_" 替换。因此替换后的字符串 $s$ 为 "_codeforces_",答案为 "_YES_"。在第二个例子中,通配符 '_*_' 可以被空字符串替换。因此替换后的字符串 $s$ 为 "_vkcup_",答案为 "_YES_"。在第三个例子中,字符串中没有通配符 '_*_',且字符串 "_v_" 和 "_k_" 不同,因此答案为 "_NO_"。在第四个例子中,不存在任何对通配符 '_*_' 的替换方式能获得字符串 $t$,因此答案为 "_NO_"。
**Definitions** Let $ s \in (\Sigma \cup \{*\})^n $ be a pattern string of length $ n $, where $ \Sigma $ is the set of lowercase Latin letters, and $ * $ denotes at most one wildcard character. Let $ t \in \Sigma^m $ be a target string of length $ m $. Let $ p \in \{0, 1\} $ indicate the presence of the wildcard: - $ p = 1 $ if $ * $ appears exactly once in $ s $, - $ p = 0 $ if $ * $ does not appear in $ s $. Let $ s_{\text{left}} \in \Sigma^* $ be the prefix of $ s $ before the wildcard (if any), and $ s_{\text{right}} \in \Sigma^* $ be the suffix of $ s $ after the wildcard (if any). If $ p = 0 $, then $ s_{\text{left}} = s $ and $ s_{\text{right}} = \varepsilon $ (empty string). **Constraints** 1. $ 1 \le n, m \le 2 \cdot 10^5 $ 2. $ s $ contains at most one occurrence of $ * $ 3. $ t $ contains only characters from $ \Sigma $ **Objective** Determine whether there exists a string $ w \in \Sigma^* $ such that: $$ s = s_{\text{left}} + * + s_{\text{right}} \quad \Rightarrow \quad s_{\text{left}} + w + s_{\text{right}} = t $$ Equivalently: - If $ p = 0 $: $ s = t $ - If $ p = 1 $: $ t = s_{\text{left}} + w + s_{\text{right}} $ for some $ w \in \Sigma^* $, i.e., $$ t[0:|s_{\text{left}}|] = s_{\text{left}} \quad \text{and} \quad t[m - |s_{\text{right}}|:m] = s_{\text{right}} \quad \text{and} \quad |s_{\text{left}}| + |s_{\text{right}}| \le m $$ **Output** $$ \begin{cases} \text{YES} & \text{if such } w \text{ exists} \\ \text{NO} & \text{otherwise} \end{cases} $$
Samples
Input #1
6 10
code*s
codeforces
Output #1
YES
Input #2
6 5
vk*cup
vkcup
Output #2
YES
Input #3
1 1
v
k
Output #3
NO
Input #4
9 6
gfgf*gfgf
gfgfgf
Output #4
NO
API Response (JSON)
{
  "problem": {
    "name": "A. Single Wildcard Pattern Matching",
    "description": {
      "content": "You are given two strings $s$ and $t$. The string $s$ consists of lowercase Latin letters and **at most one** wildcard character '_*_', the string $t$ consists only of lowercase Latin letters. The len",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1023A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given two strings $s$ and $t$. The string $s$ consists of lowercase Latin letters and **at most one** wildcard character '_*_', the string $t$ consists only of lowercase Latin letters. The len...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定两个字符串 $s$ 和 $t$。字符串 $s$ 由小写拉丁字母和至多一个通配符 '_*_' 组成,字符串 $t$ 仅由小写拉丁字母组成。字符串 $s$ 的长度为 $n$,字符串 $t$ 的长度为 $m$。\n\n字符串 $s$ 中的通配符 '_*_'(如果存在)可以被任意长度(包括空)的小写拉丁字母序列替换。$s$ 中的其他字符不能被替换。如果可以通过替换 $s$ 中的通配符 '_*_' 得到...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ s \\in (\\Sigma \\cup \\{*\\})^n $ be a pattern string of length $ n $, where $ \\Sigma $ is the set of lowercase Latin letters, and $ * $ denotes at most one wildcard character.  \nL...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments