Untitled
unknown
plain_text
a year ago
9.1 kB
7
Indexable
When a list is copied in Python3 as below, only the address to be referred to is copied. 1> b = a If the element value of List b is changed later, that of List a is also changed. For example, suppose a case where code as below is executed. 1> a = [1, 2, 3] 2> b = a 3> b[0] = 4 4> print(a) # Output: [4, 2, 3] As shown above, if the element value of List b is changed, that of List a is also changed. And if the element value of List a is changed, that of List b is also changed. [Fig. 1] To avoid such automatic change, we need to copy the value using a special function. When copying a one-dimensional list, which has integers as its elements, in Python3, we can use the copy() function. By using this function, we can copy all values without automatic change because the function copies values without copying the address to be referred to. Suppose the below case of code execution. 1> a = [1, 2, 3] 2> b = a.copy() 3> b[0] = 4 4> print(a) # Output: [1, 2, 3] Although one of the element values is changed in List b, those in List a remain unchanged. [Fig. 2] You are required to write a program which provides some of the list features. Find a better way instead of copying each and every value, which is very inefficient. Implement each required function by referring to the following API description. ※ The function signature below is based on C/C++. As for Java, refer to the provided Solution.java and UserSolution.java. The following is the description of API to be written in the User Code. void init() This function is called in the beginning of each test case. There is no list created in the beginning of the test case. void makeList(char mName[], int mLength, int mListValue[]) It is guaranteed that a list named mName does not exist. A new list named mName is created. The number of elements for mName is mLength. The “i”th element in mName is mListValue[i] ( 0 ≤ i ≤ mLength - 1 ). A task to be performed can be expressed as below. > mName = [mListValue[0], mListValue[1], …, mListValue[mLength - 1]] Parameters mName : Name of the list ( 1 ≤ length ≤ 20 ) mLength : Length of the list ( 1 ≤ mLength ≤ 200,000 ) mListValue : Value of the element ( 0 ≤ mListValue[i] ≤ 32,767 ) void copyList(char mDest[], char mSrc[], bool mCopy) It is guaranteed that mDest does not exist. It is guaranteed that mSrc exists. A new list named mDest is created. mSrc is copied into mDest. If mCopy is true, the way of copying all values is used. In this case, a task to be performed is as below. > mDest = mSrc.copy() On the other hand, if mCopy is false, the way of copying only the address is used. In this case, a task to be performed is as below. > mDest = mSrc Parameters mDest : Name of the list ( 1 ≤ length ≤ 20 ) mSrc : Name of the list ( 1 ≤ length ≤ 20 ) mCopy : Way of copying ( mCopy = true or false ) void updateElement(char mName[], int mIndex, int mValue) This function changes the mIndexth element value in mName to mValue. A task to be performed is as below. > mName[mIndex] = mValue It is guaranteed that a list named mName exists. Parameters mName : Name of the list ( 1 ≤ length ≤ 20 ) mIndex : Index of the element ( 0 ≤ mIndex ≤ length of the list - 1 ) mValue : Changed value of the element ( 0 ≤ mValue ≤ 32,767 ) int element(char mName[], int mIndex) This function returns the mIndexth element of mName. It is guaranteed that mName exists. Parameters mName : Name of the list ( 1 ≤ length ≤ 20 ) mIndex : Index of the element ( 0 ≤ mIndex ≤ length of the list - 1 ) [Example] Think of the following case in [Table 1]. Order Function return 1 init() 2 makeList(“a”, 5, {24524, 24583, 6350, 19398, 15849}) 3 element(“a”, 4) 15849 4 copyList(“b”, “a”, false) 5 updateElement(“b”, 1, 23) 6 element(“a”, 1) 23 7 copyList(“c”, “b”, true) 8 updateElement(“c”, 3, 12345) 9 element(“a”, 3) 19398 10 updateElement(“a”, 0, 99) 11 element(“c”, 0) 24524 12 element(“c”, 1) 23 [Table 1] When makeList is called at Order #2, List a is created. [Fig. 3] When element is called at Order #3, 15849, the 4th index element of List a, is returned. When copyList is called at Order #4, List b is created. When updateElement is called at Order #5, the 1st index element of List b is changed to 23. [Fig. 4] shows such change. [Fig. 4] When element is called at Order #6, 23, the 1st index element of List a, is returned. Note that both List a and b refer to the same data. When copyList is called at Order #7, List c, which is made by copying List b, is created. When updateElement is called later at Order #8, the 3rd index element of List c is changed to 12345. [Fig. 5] shows such change. [Fig. 5] When element is called at Order #9, 19398, the 3rd index element of List a, is returned. When updateElement is called at Order #10, the 0th index element of List a is changed to 99. [Fig. 6] shows such change. [Fig. 6] When element is called at Order #11, 24524, the 0th index element of List c, is returned. When element is called at Order #12, 23, the 1th index element of List c, is returned. [Constraints] 1. init() is called in the beginning of each test case. 2. For each test case, makeList() can be called less or equal to 10 times. 3. For each test case, copyList() can be called less or equal to 5,000 times. 4. For each test case, updateElement() can be called less or equal to 100,000 times. 5. For each test case, element() can be called less or equal to 400 times. 6. The name of the list is a string which consists of lowercase English letters. It has the length of minimum 1, but no larger than 20 and ends with ‘\0’. 7. The memory limit is 256MB. [Input and Output] As the input and output are processed in the provided code in the Main, they are not processed separately in the User Code. The output result for the sample input is either “#TC number 100 or 0.” It is the correct answer if the TC is 100; it is the wrong answer if it is 0. Sample Input : sample_input.txt sample_input pop-up Try Count 0Total Count 99More View Submission Time Solution Result Language C(gcc-10.3.0) C++14(gcc-10.3.0) JAVA(OpenJDK 8) Python 3.7(PyPy 7.3.4) Initialize the source code Main 1 import java.io.BufferedReader; 2 import java.io.InputStreamReader; 3 import java.util.StringTokenizer; 4 5 class Solution { 6 private final static int CMD_INIT = 100; 7 private final static int CMD_MAKE_LIST = 200; 8 private final static int CMD_COPY_LIST = 300; 9 private final static int CMD_UNDATE_ELEMENT = 400; 10 private final static int CMD_ELEMENT = 500; 11 12 private final static UserSolution usersolution = new UserSolution(); 13 14 private static int mSeed; 15 private static int pseudo_rand() 16 { 17 mSeed = mSeed * 214013 + 2531011; 18 return (mSeed >> 16) & 0x7FFF; 19 } 20 21 private static char[] mName = new char[21]; 22 private static char[] mDest = new char[21]; 23 private static char[] mSrc = new char[21]; 24 private static int[] mListValue = new int[200000]; 25 26 private static void generateName(char[] name, int seed) 27 { 28 mSeed = seed; 29 int name_len = pseudo_rand() % 20 + 1; 30 for (int i = 0; i < name_len; ++i) 31 { 32 name[i] = (char)(pseudo_rand() % 26 + 'a'); 33 } 34 name[name_len] = '\0'; 35 } 36 37 private static int generateList(int[] listValue, int seed) 38 { 39 mSeed = seed; 40 int length = pseudo_rand() << 15; 41 length = (length + pseudo_rand()) % 200000 + 1; 42 for (int i = 0; i < length; ++i) User code(UserSolution.java) 1 class UserSolution 2 { 3 public void init() 4 { 5 } 6 7 public void makeList(char mName[], int mLength, int mListValue[]) 8 { 9 } 10 11 public void copyList(char mDest[], char mSrc[], boolean mCopy) 12 { 13 } 14 15 public void updateElement(char mName[], int mIndex, int mValue) 16 { 17 } 18 19 public int element(char mName[], int mIndex) 20 { 21 return 0; 22 } 23 }
Editor is loading...
Leave a Comment