A. Bencoding

Codeforces
IDCF10124A
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Bencoding is a modern technique which is used for representing data structures as sequences of characters. It it capable of encoding strings, integers, lists and dictionaries as specified below: For example, "_4:spam_" represents the string "spam", "_0:_" represents an empty string. For example, "_i1024e_" represents the number 1024. For example, "_li101el4:spami1024eee_" represents the list "[ 101, [ "spam", 1024 ] ]". For example, "_d1:a0:1:pl1:b2:cdee_" represents the dictionary with string key "a" mapped into an empty string "", and key "p" mapped into the list "[ "b", "cd" ]". A character sequence c is called a _valid bencoded object_ if the following two conditions are met: For example, when m = 3, the sequence c = "_2:bc_" is not considered a valid bencoded object even though it represents a correctly encoded string "bc". Given m and c you have to write a program which should determine whether c is a _valid bencoded object_. If c is not a _valid bencoded object_, it also has to find the longest prefix of c which could be a prefix of some _valid bencoded object_. Formally, you should find a maximal position j within the given character sequence c, such that a prefix of c up to, but not including, character at position j could be a prefix of some _valid bencoded object_. If the given character sequence c is not a _valid bencoded object_, but the entire sequence c is a prefix of some _valid bencoded object_, then j is considered equal to the length of c. The first line of the input contains one integer m (2 ≤ m ≤ 109)  — the maximum possible length of a valid bencoded object. The second line contains a character sequence which you are to process. The sequence will only contain characters with ASCII codes from 33 to 127, inclusive. Its length will be between 1 and 106 characters. Print a single line containing word "_ok_" (without quotes) if the given input character sequence is a valid bencoded object. Otherwise, print message "_Error at position j!_". The first character of the input sequence is considered to have position "_0_". In the first sample test the given sequence is not a valid bencoded object. But its prefix "_li10e1_" can be extended to a valid bencoded object while not exceeding 14 characters in length (for example, "_li10e1:xe_"). It's not the case with longer prefixes of length 7 and more, so j = 6 in this case. ## Input The first line of the input contains one integer m (2 ≤ m ≤ 109)  — the maximum possible length of a valid bencoded object. The second line contains a character sequence which you are to process. The sequence will only contain characters with ASCII codes from 33 to 127, inclusive. Its length will be between 1 and 106 characters. ## Output Print a single line containing word "_ok_" (without quotes) if the given input character sequence is a valid bencoded object. Otherwise, print message "_Error at position j!_". The first character of the input sequence is considered to have position "_0_". [samples] ## Note In the first sample test the given sequence is not a valid bencoded object. But its prefix "_li10e1_" can be extended to a valid bencoded object while not exceeding 14 characters in length (for example, "_li10e1:xe_"). It's not the case with longer prefixes of length 7 and more, so j = 6 in this case.
**Definitions** Let $ m \in \mathbb{Z} $ with $ 2 \leq m \leq 10^9 $ be the maximum allowed length of a valid bencoded object. Let $ c \in \Sigma^* $ be the input character sequence, where $ \Sigma = \{ \text{ASCII characters from 33 to 127} \} $, and $ |c| \leq 10^6 $. A **valid bencoded object** is a string over $ \Sigma $ that conforms strictly to the Bencoding grammar: - **Integer**: $ i[+-]?[0-9]+e $ - **String**: $ [0-9]+:[^\text{e}]^* $ (length prefix followed by exactly that many characters) - **List**: $ l(\text{bencoded\_object})^*e $ - **Dictionary**: $ d(\text{string}:\text{bencoded\_object})^*e $, where keys are strings and appear in lexicographic order **Constraints** 1. $ |c| \leq 10^6 $ 2. $ m \geq 2 $ 3. The entire bencoded object (if valid) must satisfy $ |c| \leq m $ **Objective** Determine: - If $ c $ is a valid bencoded object and $ |c| \leq m $, output `"ok"`. - Otherwise, find the maximal $ j \in \{0, 1, \dots, |c|\} $ such that the prefix $ c[0:j] $ is a prefix of *some* valid bencoded object of length $ \leq m $. - If no such prefix exists (even the empty string), then $ j = 0 $. - Output `"Error at position $ j $!"`. **Note**: The parsing must be deterministic and respect Bencoding syntax rules exactly. The dictionary keys must be in lexicographically sorted order.
API Response (JSON)
{
  "problem": {
    "name": "A. Bencoding",
    "description": {
      "content": "Bencoding is a modern technique which is used for representing data structures as sequences of characters. It it capable of encoding strings, integers, lists and dictionaries as specified below:  For",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10124A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Bencoding is a modern technique which is used for representing data structures as sequences of characters. It it capable of encoding strings, integers, lists and dictionaries as specified below: \n\nFor...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ m \\in \\mathbb{Z} $ with $ 2 \\leq m \\leq 10^9 $ be the maximum allowed length of a valid bencoded object.  \nLet $ c \\in \\Sigma^* $ be the input character sequence, where $ \\Sigm...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments