B. Bill Total Value

Codeforces
IDCF727B
Time1000ms
Memory256MB
Difficulty
expression parsingimplementationstrings
English · Original
Chinese · Translation
Formal · Original
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "__name_1_price_1_name_2_price_2..._name__n__price__n__", where _name__i_ (name of the _i_\-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and _price__i_ (the price of the _i_\-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices. The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written. Otherwise, after the number of dollars a dot (decimal point) is written followed by cents **in a two-digit format** (if number of cents is between 1 and 9 inclusively, there is a leading zero). Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit. For example: * "_234_", "_1.544_", "_149.431.10_", "_0.99_" and "_123.05_" are valid prices, * "_.333_", "_3.33.11_", "_12.00_", "_.33_", "_0.1234_" and "_1.2_" are not valid. Write a program that will find the total price of all purchases in the given bill. ## Input The only line of the input contains a non-empty string _s_ with length not greater than 1000 — the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars. ## Output Print the total price **exactly in the same format** as prices given in the input. [samples]
Vasily 从商店出来后,想要重新核对账单中所有商品的总价。账单是一个字符串,其中商品名称和价格连续排列,中间没有任何空格。账单格式为 "_#cf_span[name1price1name2price2...namenpricen]_",其中 #cf_span[namei](第 #cf_span[i] 个商品的名称)是非空字符串,长度不超过 #cf_span[10],仅由小写英文字母组成;#cf_span[pricei](第 #cf_span[i] 个商品的价格)是非空字符串,由数字和点(小数点)组成。可能存在名称相同但价格不同的商品。 每个价格的书写格式如下:如果价格是整数美元,则不写美分。 否则,在美元数量后跟一个点(小数点),然后是两位数格式的美分(如果美分数在 #cf_span[1] 到 #cf_span[9] 之间,则前面补零)。 此外,美元部分的每一位数字(从低位到高位)每三位用一个点(小数点)分隔。不允许有前导零。价格总是以数字开头,也以数字结尾。 例如: 编写一个程序,计算给定账单中所有商品的总价。 输入仅包含一行,一个非空字符串 #cf_span[s],长度不超过 #cf_span[1000] —— 账单内容。 保证账单符合上述格式。保证账单中每个价格不低于 1 美分,且不超过 #cf_span[106] 美元。 请以与输入中价格完全相同的格式输出总价。 ## Input 输入仅包含一行,一个非空字符串 #cf_span[s],长度不超过 #cf_span[1000] —— 账单内容。保证账单符合上述格式。保证账单中每个价格不低于 1 美分,且不超过 #cf_span[106] 美元。 ## Output 请以与输入中价格完全相同的格式输出总价。 [samples]
Let $ s $ be a string of length $ \leq 1000 $, representing a bill in the format $ \texttt{name}_1\texttt{price}_1\texttt{name}_2\texttt{price}_2\cdots\texttt{name}_n\texttt{price}_n $, where: - Each $ \texttt{name}_i $ is a non-empty string of $ \leq 10 $ lowercase English letters. - Each $ \texttt{price}_i $ is a non-empty string of digits and dots, representing a monetary value in USD, formatted as: - Integer part (dollars): digits separated by dots every three digits from the right (thousands separators). - Optional fractional part (cents): a dot followed by exactly two digits, if non-zero. - No leading zeros in the dollar part (except for the case "0" itself, but price ≥ 1 cent). - Always starts and ends with a digit. - Format examples: `123`, `1.234`, `12.345.678`, `12.34`, `1.234.567.890.12`. **Goal**: Parse $ s $ to extract all $ \texttt{price}_i $, convert each to a numeric value (in cents), sum them, and output the total in the same string format as the input prices. --- **Formal Representation**: Let $ s \in (\Sigma_{\text{letter}} \cup \Sigma_{\text{digit}} \cup \{.\})^* $, with $ |\texttt{length}(s)| \leq 1000 $, and $ \Sigma_{\text{letter}} = \{a,b,\dots,z\} $, $ \Sigma_{\text{digit}} = \{0,1,\dots,9\} $. Define a parsing function $ \texttt{parse\_price}(p) $ that takes a price string $ p $ and returns the value in cents: - Remove all dots from $ p $ to obtain a digit string $ d $. - If $ |d| \geq 3 $, then the last two digits are cents; the rest are dollars. - Else (i.e., $ |d| \leq 2 $), the entire string is cents. - Then: $$ \texttt{parse\_price}(p) = \begin{cases} \texttt{int}(d) & \text{if } |d| \leq 2 \\ \texttt{int}(d[0 : |d|-2]) \times 100 + \texttt{int}(d[|d|-2 : ]) & \text{if } |d| \geq 3 \end{cases} $$ Let $ P = \{ p_1, p_2, \dots, p_n \} $ be the sequence of price strings extracted from $ s $, parsed greedily by scanning for the next name (sequence of letters) followed by a price (sequence of digits and dots, ending before next letter or end of string). Let $ T = \sum_{i=1}^n \texttt{parse\_price}(p_i) $ be the total in cents. Define a formatting function $ \texttt{format\_price}(T) $ that returns a string representing $ T $ cents in the required format: - Let $ \texttt{dollars} = \left\lfloor \frac{T}{100} \right\rfloor $, $ \texttt{cents} = T \bmod 100 $. - Format dollars: - Convert $ \texttt{dollars} $ to string. - Insert dots every three digits from the right (i.e., reverse string, group in threes, join with dots, reverse back). - If $ \texttt{dollars} = 0 $, output "0". - Format cents: - If $ \texttt{cents} = 0 $, output only the formatted dollars. - Else, output formatted dollars + "." + two-digit cents (with leading zero if needed). Output: $ \texttt{format\_price}(T) $ --- **Input**: A string $ s $ of length $ \leq 1000 $, matching the described bill format. **Output**: A string representing the total price in the exact same format as individual prices in $ s $.
Samples
Input #1
chipsy48.32televizor12.390
Output #1
12.438.32
Input #2
a1b2c3.38
Output #2
6.38
Input #3
aa0.01t0.03
Output #3
0.04
API Response (JSON)
{
  "problem": {
    "name": "B. Bill Total Value",
    "description": {
      "content": "Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row with",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF727B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row with...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vasily 从商店出来后,想要重新核对账单中所有商品的总价。账单是一个字符串,其中商品名称和价格连续排列,中间没有任何空格。账单格式为 \"_#cf_span[name1price1name2price2...namenpricen]_\",其中 #cf_span[namei](第 #cf_span[i] 个商品的名称)是非空字符串,长度不超过 #cf_span[10],仅由小写英文字母组成;#cf...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ s $ be a string of length $ \\leq 1000 $, representing a bill in the format $ \\texttt{name}_1\\texttt{price}_1\\texttt{name}_2\\texttt{price}_2\\cdots\\texttt{name}_n\\texttt{price}_n $, where:\n\n- Each...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments