#P9680. string[_view]
string[_view]
Background
C++'s string class is a powerful string class. However, because its string algorithms are tied to its memory management mechanism, it is inefficient when dealing with C-style strings.
To solve this problem, the C++17 standard introduced the string_view type, separating memory management from string algorithms, thus better adapting to processing C-style strings.
Problem Description
You need to simulate a simple C++ program. Each line of the program must be in one of the following two forms:
string <variable-name>(<initializer>);string_view <variable-name>(<initializer>);
Here, variable-name is the declared variable name (guaranteed not to have appeared before, and its length does not exceed ). initializer is the content used to initialize this variable, which can be:
- A string literal, i.e., a string enclosed in double quotes (in the form
"abc"). - A previously appeared variable name
source. In this case, you should assign the string corresponding tosourcetovariable-name.
Specifically, assigning any string to type string will perform character copies, while assigning to type string_view will not copy any characters. Here, is the length of string .
You need to compute the total number of character copies in the program.
Input Format
The first line contains an integer , representing the number of lines in the program.
In the next lines, a piece of code is given.
Output Format
Output an integer, representing the total number of character copies.
6
string a("cxyakioi");
string_view b("cxyakapio");
string c(b);
string_view d(a);
string_view cxyakioi(c);
string cxyakapio(d);
25
Hint
For each test case, it is guaranteed that the length of the code does not exceed (excluding newline characters).
It is guaranteed that the string literals (excluding the surrounding quotes) and variable names contain only Latin letters, and the given code strictly satisfies the requirements of the problem.
Subtasks
| # | Special Property | Score |
|---|---|---|
| 0 | Sample | 0 |
| 1 | All variables are string_view | 10 |
| 2 | Only string literals are used | 20 |
| 3 | - | 70 |
Good news: GCC 9.3.0 supports string_view.
Bad news: NOI does not enable C++17.
Translated by ChatGPT 5