| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11267 | Equivalent relation |
|
| 11288 | Text Editor 2 |
|
| 11756 | Dynamic 2D array |
|
Description
There are N integer pointers, indexed from 0 to N-1 (N<100). Each pointer initially points to an integer of value 0.
There are two kinds of instructions. The instruction “S n k” is used to set the integer, which pointer n points to, to be k. For example, S 1 10 means that the integer that pointer 1 points to is set to 10. And the instruction “P n m” means that pointer n points to the integer that pointer m points. For example, P 2 1 means that pointer 2 points to the integer that pointer 1 points to. After P 2 1, pointer 2 and pointer 1 point to the same integer, which is pointed by pointer 1.
Note that you don't have to change all the pointers if one pointer changes its target. The following table is an example. The instructions are P 1 2 and then P 2 3. You do not have to change the target of pointer 1.
|
instruction |
Description |
|
P 1 2 |
Pointer 1 points to the integer that pointer 2 points to.
|
|
P 2 3 |
Pointer 2 points to the integer that pointer 3 points to. And you don’t have to change the target of pointer 1. |
Finally, output all the values that pointers 0 to N-1 point to in order.
Note that
1. This problem involves three files.
- function.h: Function definition of execInstruct.
- function.c: Function describe of execInstruct.
- main.c: A driver program to test your implementation.
You will be provided with main.c and function.h, and asked to implement function.c.
2. For OJ submission:
Step 1. Submit only your function.c into the submission block. (Please choose c compiler)
Step 2. Check the results and debug your program if necessary.
Hints:
main.c
#include <stdio.h>
#include "function.h"
#define SIZE 100
int main() {
int *ptrArr[SIZE];
int dataArr[SIZE] = {0};
char inst;
int dataNum, instNum;
int param1, param2;
int i;
/* input */
scanf("%d %d", &dataNum, &instNum);
/* initialize the ptrArr */
for (i = 0; i < dataNum; i++)
ptrArr[i] = &dataArr[i];
for (i = 0; i < instNum; i++) {
scanf(" %c %d %d", &inst, ¶m1, ¶m2);
execInst(ptrArr, inst, param1, param2);
}
/* output */
for (i = 0; i < dataNum - 1; i++) {
printf("%d ", *ptrArr[i]);
}
printf("%d", *ptrArr[i]);
return 0;
}
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
void execInst(int *ptrArr[], char inst, int param1, int param2);
#endif
Input
The first line contains two positive X and Y. X indicates the size of data. Y indicates that there are Y instructions needed to be done.
The next Y lines contain the instructions.
Output
All the values that pointers 0 to pointer N-1 point to in order. Each value is seperated by a blank ' '.
# Note that there is no '\n' at the end of the output.
Sample Input Download
Sample Output Download
Partial Judge Code
11267.cPartial Judge Header
11267.hTags
Discuss
Description
In this problem we simulate a simple text editor. Given two lines of keyboard input texts and commands, output the final text content.
The text editing rules are defined as following:
1. Normal alphabetical input and whitespace input (abcdefg…. and ‘ ‘) in the first line..
And a series of two special commands started with a backslash(/) character in the second line.
2. The backspace command which deletes a letter before the cursor (/b)
3. The navigating command which move the cursor to the right(/r)
4. The cursor is at the beginning of the text initially.
For example, for the following input:
The quick brown fox jumps over the lazy dog
/b/r/r/r/b
You should first store the "The quick brown fox jumps over the lazy dog" in the input char array and set cursor=0.
Then you can simulate the operation of the text editor.
The size of the text content is less than or equal to 500 characters, and there will be less than or equal to 500 commands when simulating.
Hint:
#include <stdio.h>
#define MAX_SIZE 1001
char content[MAX_SIZE];
char input[MAX_SIZE];
char command[MAX_SIZE];
int main()
{
gets(input);
gets(command);
/* your code here */
printf("%s", content);
return 0;
}
Input
The first line is the text content which should be edited.
The second line is the commands.
There is always a valid command(/b /r) right after the backslash character.
Output
The final text content, and there should be no '\n' at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this problem, you are asked to design two functions
1.
unsigned** new_2d_array(unsigned n,unsigned m);
malloc an n*m 2D unsigned array, and then return its address. The main function will check the correctness of your array.
2.
void delete_2d_array(unsigned **arr);
Free the memory space of your array that was previously allocated by using malloc. Be careful about the memory uage of your program allocated dynamically so as to avoid MLE.
The two functions have been declared in function.h, and you are asked to complete the function definitions in function.c.
Your program should construct the 2D array by using only two malloc function calls. Notice that malloc is a time-consuming operation.
Note: for OJ submission:
Step 1. Submit only your function.c into the submission block. (Please choose C compiler)
Step 2. Check the results and debug your program if necessary.
Input
Please refer to the main function.
The input only has one line, consisting of four positive integers t,n,m,r separated by space characters, where t is the number of tests, (n,m) represents the array size, and r is a parameter for testing.
Note that n*m<=100000000 and t*n*m<=100000000
Output
In order to test your array's correctness, we will use your array to do some computations and output the results.