#P9680. string[_view]

    ID: 10092 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟字符串洛谷原创O2优化洛谷月赛

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 1010). 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 to source to variable-name.

Specifically, assigning any string ss to type string will perform s|s| character copies, while assigning to type string_view will not copy any characters. Here, s|s| is the length of string ss.

You need to compute the total number of character copies in the program.

Input Format

The first line contains an integer LL, representing the number of lines in the program.

In the next LL 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 10410^4 (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