C. Vladik and chat

Codeforces
IDCF754C
Time2000ms
Memory256MB
Difficulty
brute forceconstructive algorithmsdpimplementationstrings
English · Original
Chinese · Translation
Formal · Original
Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download _t_ chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that **there could not be two or more messages in a row with the same sender**. Moreover, **a sender never mention himself in his messages**. Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages. He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats! ## Input The first line contains single integer _t_ (1 ≤ _t_ ≤ 10) — the number of chats. The _t_ chats follow. Each chat is given in the following format. The first line of each chat description contains single integer _n_ (1 ≤ _n_ ≤ 100) — the number of users in the chat. The next line contains _n_ space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer _m_ (1 ≤ _m_ ≤ 100) — the number of messages in the chat. The next _m_ line contain the messages in the following formats, one per line: * _<username>:<text>_ — the format of a message with known sender. The username should appear in the list of usernames of the chat. * _<?>:<text>_ — the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters _'.'_ (dot), _','_ (comma), _'!'_ (exclamation mark), _'?'_ (question mark) and _' '_ (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "_Vasya, masha13 and Kate!_" can mention users "_Vasya_", "_masha13_", "_and_" and "_Kate_", but not "_masha_". It is guaranteed that in each chat **no known sender mention himself in his messages** and there are no two neighboring messages with the same known sender. ## Output Print the information about the _t_ chats in the following format: If it is not possible to recover senders, print single line "_Impossible_" for this chat. Otherwise print _m_ messages in the following format: _<username>:<text>_ If there are multiple answers, print any of them. [samples]
最近,Vladik 发现了一种新的娱乐方式——为社交网络编写聊天机器人。他希望在机器人中使用机器学习,因此现在他需要为它们准备一些学习数据。 首先,他需要下载 #cf_span[t] 个聊天记录。Vladik 编写了一个脚本来下载这些聊天记录,但出了些问题:某些消息没有发送者信息。已知,如果一个人连续发送多条消息,它们会被合并为一条消息。这意味着 *连续的两条或更多消息不可能来自同一个发送者*。此外,*发送者不会在自己的消息中提及自己*。 Vladik 希望恢复每条消息的发送者,使得每两条相邻消息的发送者不同,且没有发送者在自己的消息中提及自己。 他不知道如何解决这个问题,因此向你求助。请帮助 Vladik 恢复每个聊天中的发送者信息! 第一行包含一个整数 #cf_span[t](#cf_span[1 ≤ t ≤ 10])——聊天的数量。接下来是 #cf_span[t] 个聊天。每个聊天的格式如下: 每个聊天描述的第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])——聊天中的用户数量。 下一行包含 #cf_span[n] 个用空格分隔的不重复用户名。每个用户名由小写和大写英文字母及数字组成。用户名不能以数字开头。即使两个用户名仅大小写不同,它们也被视为不同。用户名长度为正数,且不超过 #cf_span[10] 个字符。 下一行包含一个整数 #cf_span[m](#cf_span[1 ≤ m ≤ 100])——聊天中的消息数量。接下来的 #cf_span[m] 行每行包含一条消息,格式如下: 消息文本可以包含小写和大写英文字母、数字、字符 _'.'_(点)、_','_(逗号)、_'!'_(感叹号)、_'?'_(问号)和 _' '_(空格)。文本不包含尾随空格。文本长度为正数,且不超过 #cf_span[100] 个字符。 我们称一条文本“提及”某个用户,当且仅当该用户的用户名作为独立单词出现在文本中。换句话说,用户名必须出现在一个位置,使得其前后两个字符(如果存在)都不是英文字母或数字。例如,文本 "_Vasya, masha13 and Kate!_" 可以提及用户 "_Vasya_"、"_masha13_"、"_and_" 和 "_Kate_",但不能提及 "_masha_"。 保证在每个聊天中,*已知发送者不会在自己的消息中提及自己*,且*没有两个相邻消息具有相同的已知发送者*。 请按以下格式输出 #cf_span[t] 个聊天的信息: 如果无法恢复发送者信息,请为该聊天输出一行 "_Impossible_"。否则,请输出 #cf_span[m] 条消息,格式如下: _:_ 如果有多个答案,输出任意一个即可。 ## Input 第一行包含一个整数 #cf_span[t](#cf_span[1 ≤ t ≤ 10])——聊天的数量。接下来是 #cf_span[t] 个聊天。每个聊天的格式如下:每个聊天描述的第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])——聊天中的用户数量。下一行包含 #cf_span[n] 个用空格分隔的不重复用户名。每个用户名由小写和大写英文字母及数字组成。用户名不能以数字开头。即使两个用户名仅大小写不同,它们也被视为不同。用户名长度为正数,且不超过 #cf_span[10] 个字符。下一行包含一个整数 #cf_span[m](#cf_span[1 ≤ m ≤ 100])——聊天中的消息数量。接下来的 #cf_span[m] 行每行包含一条消息,格式如下: _:_ —— 具有已知发送者的消息格式。用户名必须出现在聊天的用户名列表中。 _:_ —— 具有未知发送者的消息格式。消息文本可以包含小写和大写英文字母、数字、字符 _'.'_(点)、_','_(逗号)、_'!'_(感叹号)、_'?'_(问号)和 _' '_(空格)。文本不包含尾随空格。文本长度为正数,且不超过 #cf_span[100] 个字符。 我们称一条文本“提及”某个用户,当且仅当该用户的用户名作为独立单词出现在文本中。换句话说,用户名必须出现在一个位置,使得其前后两个字符(如果存在)都不是英文字母或数字。例如,文本 "_Vasya, masha13 and Kate!_" 可以提及用户 "_Vasya_"、"_masha13_"、"_and_" 和 "_Kate_",但不能提及 "_masha_"。 保证在每个聊天中,*已知发送者不会在自己的消息中提及自己*,且*没有两个相邻消息具有相同的已知发送者*。 ## Output 请按以下格式输出 #cf_span[t] 个聊天的信息:如果无法恢复发送者信息,请为该聊天输出一行 "_Impossible_"。否则,请输出 #cf_span[m] 条消息,格式如下: _:_ 如果有多个答案,输出任意一个即可。 [samples]
**Definitions** Let $ t \in \mathbb{Z} $ be the number of chats. For each chat $ k \in \{1, \dots, t\} $: - Let $ n_k \in \mathbb{Z} $ be the number of users. - Let $ U_k = \{u_1, u_2, \dots, u_{n_k}\} $ be the set of distinct usernames. - Let $ m_k \in \mathbb{Z} $ be the number of messages. - Let $ M_k = (m_{k,1}, m_{k,2}, \dots, m_{k,m_k}) $ be the sequence of message texts. - For each message $ m_{k,i} $, let $ \text{Mentions}(m_{k,i}) \subseteq U_k $ be the set of usernames mentioned in $ m_{k,i} $, defined as substrings matching a username bounded by non-alphanumeric characters or string boundaries. - Let $ s_{k,i} \in U_k \cup \{\bot\} $ denote the known sender of message $ i $, where $ \bot $ means unknown. **Constraints** 1. $ 1 \le t \le 10 $ 2. For each chat $ k $: - $ 1 \le n_k \le 100 $ - $ 1 \le m_k \le 100 $ - Each username has length $ \in [1, 10] $, consists of letters/digits, does not start with a digit. - Each message text has length $ \in [1, 100] $, contains only allowed characters. - For each known sender $ s_{k,i} \ne \bot $: - $ s_{k,i} \notin \text{Mentions}(m_{k,i}) $ (sender does not mention self). - If $ i > 1 $, then $ s_{k,i} \ne s_{k,i-1} $ (no two consecutive same senders). **Objective** For each chat $ k $, assign a sender $ \sigma_{k,i} \in U_k $ to each message $ i \in \{1, \dots, m_k\} $ such that: 1. If $ s_{k,i} \ne \bot $, then $ \sigma_{k,i} = s_{k,i} $. 2. $ \sigma_{k,i} \notin \text{Mentions}(m_{k,i}) $ for all $ i $. 3. $ \sigma_{k,i} \ne \sigma_{k,i-1} $ for all $ i \in \{2, \dots, m_k\} $. If such an assignment exists, output $ \sigma_{k,1}, \dots, \sigma_{k,m_k} $. Otherwise, output "Impossible".
Samples
Input #1
1
2
Vladik netman
2
?: Hello, Vladik!
?: Hi
Output #1
netman: Hello, Vladik!
Vladik: Hi
Input #2
1
2
netman vladik
3
netman:how are you?
?:wrong message
vladik:im fine
Output #2
Impossible
Input #3
2
3
netman vladik Fedosik
2
?: users are netman, vladik, Fedosik
vladik: something wrong with this chat
4
netman tigerrrrr banany2001 klinchuh
4
?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?
klinchuh: yes, coach!
?: yes, netman
banany2001: yes of course.
Output #3
Impossible
netman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?
klinchuh: yes, coach!
tigerrrrr: yes, netman
banany2001: yes of course.
API Response (JSON)
{
  "problem": {
    "name": "C. Vladik and chat",
    "description": {
      "content": "Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF754C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.\n\nAt first, he...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "最近,Vladik 发现了一种新的娱乐方式——为社交网络编写聊天机器人。他希望在机器人中使用机器学习,因此现在他需要为它们准备一些学习数据。\n\n首先,他需要下载 #cf_span[t] 个聊天记录。Vladik 编写了一个脚本来下载这些聊天记录,但出了些问题:某些消息没有发送者信息。已知,如果一个人连续发送多条消息,它们会被合并为一条消息。这意味着 *连续的两条或更多消息不可能来自同一个发送者*。...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ t \\in \\mathbb{Z} $ be the number of chats.  \nFor each chat $ k \\in \\{1, \\dots, t\\} $:  \n- Let $ n_k \\in \\mathbb{Z} $ be the number of users.  \n- Let $ U_k = \\{u_1, u_2, \\dots, ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments