C. Hockey

Codeforces
IDCF96C
Time2000ms
Memory256MB
Difficulty
implementationstrings
English · Original
Chinese · Translation
Formal · Original
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name _w_ and the collection of forbidden substrings _s_1, _s_2, ..., _s__n_. All those strings consist of uppercase and lowercase Latin letters. String _w_ has the length of |_w_|, its characters are numbered from 1 to |_w_|. First Petya should find all the occurrences of forbidden substrings in the _w_ string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "_aBC_" and "_ABc_" are considered equal. After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position _i_ should be replaced by any other one if for position _i_ in string _w_ there exist pair of indices _l_, _r_ (1 ≤ _l_ ≤ _i_ ≤ _r_ ≤ |_w_|) such that substring _w_\[_l_ ... _r_\] is contained in the collection _s_1, _s_2, ..., _s__n_, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring. Letter _letter_ (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the _letter_ occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically. Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them. ## Input The first line contains the only integer _n_ (1 ≤ _n_ ≤ 100) — the number of forbidden substrings in the collection. Next _n_ lines contain these substrings. The next line contains string _w_. All those _n_ + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter _letter_. ## Output Output the only line — Petya's resulting string with the maximum number of letters _letter_. If there are several answers then output the one that comes first lexicographically. The lexicographical comparison is performed by the standard < operator in modern programming languages. The line _a_ is lexicographically smaller than the line _b_, if _a_ is a prefix of _b_, or there exists such an _i_ (1 ≤ _i_ ≤ |_a_|), that _a__i_ < _b__i_, and for any _j_ (1 ≤ _j_ < _i_) _a__j_ = _b__j_. |_a_| stands for the length of string _a_. [samples]
Petya 非常喜欢冰球。一天,当他观看一场冰球比赛时,他睡着了。Petya 梦见自己被任命为更改一支冰球队的名称。因此,Petya 被给予了原始队名 #cf_span[w] 和一组禁用子串 #cf_span[s1, s2, ..., sn]。所有这些字符串均由大写和小写拉丁字母组成。字符串 #cf_span[w] 的长度为 #cf_span[|w|],其字符编号从 #cf_span[1] 到 #cf_span[|w|]。 首先,Petya 应找出 #cf_span[w] 字符串中所有禁用子串的出现位置。在搜索子串时,字母的大小写不应被考虑。也就是说,字符串 "_aBC_" 和 "_ABc_" 被视为相等。 之后,Petya 应替换所有被这些出现位置覆盖的字母。更正式地说:如果在字符串 #cf_span[w] 中,位置 #cf_span[i] 存在一对索引 #cf_span[l, r](#cf_span[1 ≤ l ≤ i ≤ r ≤ |w|]),使得子串 #cf_span[w[l ... r]] 在集合 #cf_span[s1, s2, ..., sn] 中(使用大小写不敏感比较),则位置 #cf_span[i] 的字母应被替换为任意其他字母。替换时,字母的大小写应保持不变。Petya 不允许替换未被任何禁用子串覆盖的字母。 字母 #cf_span[letter](大写或小写)对冰球运动员来说是幸运的。因此,Petya 应进行修改,使得结果字符串中 #cf_span[letter] 出现的次数尽可能多。帮助 Petya 找到这样的结果字符串。如果有多个这样的字符串,则找出字典序最小的那个。 注意,替换过程只进行一次,不会重复。也就是说,即使 Petya 的替换后字符串中出现了新的禁用子串,Petya 也不予理会。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])——禁用子串集合中的子串数量。接下来 #cf_span[n] 行包含这些子串。下一行包含字符串 #cf_span[w]。这 #cf_span[n + 1] 行均为非空字符串,由大写和小写拉丁字母组成,长度不超过 #cf_span[100]。最后一行包含一个小写字母 #cf_span[letter]。 输出仅一行——Petya 的结果字符串,其中 #cf_span[letter] 出现次数最多。如果有多个答案,则输出字典序最小的那个。 字典序比较由现代编程语言中的标准 < 运算符执行。字符串 #cf_span[a] 字典序小于字符串 #cf_span[b],当且仅当 #cf_span[a] 是 #cf_span[b] 的前缀,或者存在某个 #cf_span[i](#cf_span[1 ≤ i ≤ |a|]),使得 #cf_span[ai < bi],且对于任意 #cf_span[j](#cf_span[1 ≤ j < i])都有 #cf_span[aj = bj]。#cf_span[|a|] 表示字符串 #cf_span[a] 的长度。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])——禁用子串集合中的子串数量。接下来 #cf_span[n] 行包含这些子串。下一行包含字符串 #cf_span[w]。这 #cf_span[n + 1] 行均为非空字符串,由大写和小写拉丁字母组成,长度不超过 #cf_span[100]。最后一行包含一个小写字母 #cf_span[letter]。 ## Output 输出仅一行——Petya 的结果字符串,其中 #cf_span[letter] 出现次数最多。如果有多个答案,则输出字典序最小的那个。字典序比较由现代编程语言中的标准 < 运算符执行。字符串 #cf_span[a] 字典序小于字符串 #cf_span[b],当且仅当 #cf_span[a] 是 #cf_span[b] 的前缀,或者存在某个 #cf_span[i](#cf_span[1 ≤ i ≤ |a|]),使得 #cf_span[ai < bi],且对于任意 #cf_span[j](#cf_span[1 ≤ j < i])都有 #cf_span[aj = bj]。#cf_span[|a|] 表示字符串 #cf_span[a] 的长度。 [samples]
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of forbidden substrings. Let $ S = \{s_1, s_2, \dots, s_n\} $ be the set of forbidden substrings, where each $ s_i \in \Sigma^* $, $ \Sigma = \{a, \dots, z, A, \dots, Z\} $. Let $ w \in \Sigma^* $ be the original team name, with $ |w| = m $. Let $ c \in \{a, \dots, z\} $ be the target lucky letter. Let $ \text{lower}(x) $ denote the case-insensitive version of string $ x $, i.e., all characters converted to lowercase. **Constraints** 1. $ 1 \leq n \leq 100 $ 2. $ 1 \leq |s_i| \leq 100 $ for all $ i \in \{1, \dots, n\} $ 3. $ 1 \leq |w| \leq 100 $ 4. All strings consist only of uppercase and lowercase Latin letters. **Covered Positions** Define the set of *covered indices* $ I \subseteq \{1, \dots, m\} $ as: $$ I = \left\{ i \in \{1, \dots, m\} \mid \exists\, l, r \in \mathbb{Z},\ 1 \leq l \leq i \leq r \leq m,\ \text{such that}\ \text{lower}(w[l:r]) \in \text{lower}(S) \right\} $$ where $ w[l:r] $ denotes the substring from index $ l $ to $ r $ (inclusive), and $ \text{lower}(S) = \{ \text{lower}(s) \mid s \in S \} $. **Replacement Rule** For each position $ i \in I $, the character $ w_i $ must be replaced by some character $ x \in \Sigma $ such that: - $ x $ has the same case as $ w_i $ (i.e., if $ w_i $ is uppercase, so is $ x $; if lowercase, so is $ x $). - $ x \neq w_i $. For each position $ i \notin I $, $ w_i $ remains unchanged. **Objective** Maximize the total number of occurrences of the letter $ c $ in the resulting string $ w' $, where $ c $ may appear in either case depending on context, but: - Only lowercase $ c $ counts as the lucky letter (as specified: “letter $ c $” is lowercase). → **Clarification**: The problem states “Letter `letter` (uppercase or lowercase) is considered lucky”, and the input `letter` is given in lowercase. → **Interpretation**: Both $ c $ (lowercase) and $ C $ (uppercase) count as lucky. Thus, define the *lucky count* of a string $ w' $ as: $$ L(w') = \#\{ i \in \{1, \dots, m\} \mid w'_i = c \lor w'_i = \text{upper}(c) \} $$ **Goal** Find $ w' \in \Sigma^* $ such that: 1. $ w' $ satisfies the replacement rule above. 2. $ L(w') $ is maximized. 3. Among all such $ w' $, choose the lexicographically smallest one. **Lexicographic Order** Defined as standard string comparison: $ w'_1 < w'_2 $ iff $ \exists i $ such that $ w'_1[j] = w'_2[j] $ for all $ j < i $, and $ w'_1[i] < w'_2[i] $ (by ASCII order).
Samples
Input #1
3
bers
ucky
elu
PetrLoveLuckyNumbers
t
Output #1
PetrLovtTttttNumtttt
Input #2
4
hello
party
abefglghjdhfgj
IVan
petrsmatchwin
a
Output #2
petrsmatchwin
Input #3
2
aCa
cba
abAcaba
c
Output #3
abCacba
API Response (JSON)
{
  "problem": {
    "name": "C. Hockey",
    "description": {
      "content": "Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name _",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF96C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name _...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Petya 非常喜欢冰球。一天,当他观看一场冰球比赛时,他睡着了。Petya 梦见自己被任命为更改一支冰球队的名称。因此,Petya 被给予了原始队名 #cf_span[w] 和一组禁用子串 #cf_span[s1, s2, ..., sn]。所有这些字符串均由大写和小写拉丁字母组成。字符串 #cf_span[w] 的长度为 #cf_span[|w|],其字符编号从 #cf_span[1] 到 #...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of forbidden substrings.  \nLet $ S = \\{s_1, s_2, \\dots, s_n\\} $ be the set of forbidden substrings, where each $ s_i \\in \\Sigma^* $, $ \\Sigma...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments