#P8791. [蓝桥杯 2022 国 AC] 内存空间
[蓝桥杯 2022 国 AC] 内存空间
Problem Description
Xiao Lan recently likes to calculate how much memory space the variables defined in his code take up.
To simplify the problem, there are only three types of variables:
int: an integer variable. One int variable takes Byte of memory space.
long: a long integer variable. One long variable takes Byte of memory space.
String: a string variable. The space used depends on the string length. Let the string length be , then the string takes Byte of memory space. If the string length is , then it takes Byte of memory space.
There are only two forms of variable definition statements. The first form is:
type var1=value1,var2=value2...;
This defines several variables of type type: var1, var2, ..., and initializes them with value1, value2, ....
Multiple variables are separated by ,, and the statement ends with ;. type can be int, long, or String. For example, int a=1,b=5,c=6; takes Byte; long a=1,b=5; takes Byte; String s1="",s2="hello",s3="world"; takes Byte.
The second form is:
type[] arr1=new type[size1],arr2=new type[size2]…;
This defines several one-dimensional array variables of type type: arr1, arr2, ..., and the array sizes are size1, size2, .... Multiple variables are separated by ,, and the statement ends with ;. Here type can only be int or long. For example, int[] a1=new int[10] takes Byte; long[] a1=new long[10],a2=new long[10]; takes Byte.
It is known that Xiao Lan has variable definition statements. Please help him count the total memory space used.
The result should be formatted as , where , , , are the computed values, and , , , are the units. Prefer using larger units: , , , where B means Byte. If some of , , , are , then you do not need to output those numbers and their units.
The problem guarantees that each line contains only one variable definition statement, and every statement satisfies the formats described above. All variable names are valid and do not repeat. The testdata is very regular and similar to the examples above: except for one space after the type, and one space after new when defining arrays, there will be no extra spaces.
Input Format
The first line contains an integer , meaning there are variable definition statements.
The next lines each contain one variable definition statement.
Output Format
Output one line containing a string that represents the total size of memory space used by all statements.
1
long[] nums=new long[131072];
1MB
4
int a=0,b=0;
long x=0,y=0;
String s1="hello",s2="world";
long[] arr1=new long[100000],arr2=new long[100000];
1MB538KB546B
Hint
Sample Explanation
In Sample 1, the space used is , which converts to exactly . The numbers before the other three units , , are all , so they are not printed.
In Sample 2, the space used is $4 \times 2 + 8 \times 2 + 10 + 8 \times 100000 \times 2$ B, which converts to .
Constraints and Conventions
For all test cases, . The length of each variable definition statement does not exceed . The length of every variable name does not exceed , and all names consist of lowercase letters and digits. For integer variables, the initialized values are decimal integers within their representable ranges, and the initialized values will not be variables. For String variables, the initialized content length does not exceed , and the content contains only lowercase letters and digits, and the initialized values will not be variables. For array variables, the array length is an integer in the range , and the array length will not be a variable. The total memory space used by all variables defined in the statements is greater than B and less than GB.
Lanqiao Cup 2022 National Final, Group A, Problem C (Group C, Problem D).
Translated by ChatGPT 5