# | Problem | Pass Rate (passed user / total user) |
---|---|---|
11711 | Dynamic 3D array |
|
12568 | Reverse Linked List ver 2 |
|
12589 | Small Cat Society |
|
13414 | String Calculator |
|
13415 | Flip Game 3D |
|
13422 | Cheat Sheet (I2P1-final) |
|
Description
In this problem, you are asked to design two functions
1.
unsigned*** new_3d_array(unsigned n,unsigned m,unsigned k);
malloc an n*m*k 3D unsigned array, and then return its address. The main function will check the correctness of your array.
2.
void delete_3d_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 3D array by using only three 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 five positive integers t,n,m,k,r separated by space characters, where t is the number of tests, (n,m,k) represents the array size, and r is a parameter for testing.
Note that n*m*k<=10000000 and t*n*m*k<=60000000
Output
In order to test your array's correctness, we will use your array to do some computations and output the results.
Sample Input Download
Sample Output Download
Partial Judge Code
11711.cPartial Judge Header
11711.hTags
Discuss
Description
Given several operations, push x
, pop
, print
, reverse
, create a linked list dynamicly.
- push x: Add one Node (with data = x) at the front of linked list.
- pop: Delete the Node at the front of linked list.
- reverse: reverse the current linked list
- print: output the linked list in given format.
You have to implement 3 functions:
1. void Push(Node** ptr_head,int x)
2. void Pop(Node** ptr_head)
3. void Reverse_List(Node** ptr_head)
Note: Modify the Node* head
by Node** ptr_head
Input
There’re operations on several lines.
All operations are one of push x
, pop
, print
, reverse
.
It’s guaranteed that:
- Number of operations is less than 5,000,000
- Integer x is in [−1000,1000]
- The maximum length of linked list is less than 10,000.
Output
Output the linked list for every print
.
Sample Input Download
Sample Output Download
Partial Judge Code
12568.cPartial Judge Header
12568.hTags
Discuss
Description
Wild cats take care of each other in the wild. However, when winter comes, the preys are not enough to feed all the cats.
In this problem, you are asked to help decide who can enjoy the precious food for three different categories of cats: apprentice, elder, and kitty.
First, the cats dine according to the following order of their occupations:
1. elder
2. kitty
3. apprentice
Then, except that the apprentices have the dining priority of the young over the old, for the other two occupations, the old have higher priority. If the occupations and the ages of two or more cats are the same, they will dine in lexicographic order according to their names.
Input
There are multiple test cases.
The first line of each test case contains two integers N and M, indicating the number of cats and the portions of food respectively, where 0<N,M<=10000.
The next N lines are the information of each cat, including name, occupation, and age.
The length of the names will not exceed 30 letters and will contain no spaces.
Output
Please output the cats that could eat the food in order, each name a line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this problem, you need to finish a string calculator.
The calculator has four string operators:
- a + b: Concatenate the string b after the string a.
- a - b: If string b has appeared in string a, delete the first appearance of b in a, otherwise output "error".
- a / b: The number of occurrences of string b in string a.
- a @ b1_b2_b3_...bk: For all strings bi concatenated with _, if a has appeared in string bi, add bi to a list, output the list in this order:
- If |x| > |y|: x is listed before y
- If |x| = |y|: follows the dictionary order
Input
The first line contains an integer N
The following N lines, each line contains an operation.
testcase:
(2/12) 0 < N < 100, 0 < |a|, |b| <= 10, only "+" operation
(3/12) 0 < N < 100, 0 < |a|, |b| <= 10, only "-" operation
(3/12) 0 < N < 100, 0 < |a|, |b| <= 10, only "/" operation
(2/12) 0 < N < 100, 0 < |a|, |bi| <= 10, 0 < k <= 500 , only "@" operation
(2/12) 0 < N < 100, 0 < |a|, |bi| <= 10, 0 < k <= 10000 , only "@" operation
Output
The output contains N lines, output the calculated result.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Flip game 3D is played on a cube H x N x M with two-sided pieces placed on each of its H x N x M slots. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip some pieces, thus changing the colors of their upper sides from black to white and vice versa. The pieces to be flipped on the H x N x M field are chosen every round according to the following rules:
- Choose any one of the H x N x M pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, to the bottom, to the front, and to the back of the chosen piece (if there are any). That is, depending on the position of the chosen piece, there can be totally 4 to 7 pieces flipped.
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
If it's impossible, print "oops".
Useful Info.
-
3D Cubic Structure char s[H+2][N+2][M+2];
-
Sequential Piece Numbering
-
Sequential Piece Numbering: Generalized
Input
The first line contains a single integer T (1 ≤ T ≤ 100). Then T test cases follow.
For each test case:
- The first line gives three integers H, N and M (1 ≤ max(H,N,M) ≤ 20).
- The remainder gives H rectangles, each of which is represented by N lines, where each line consists of M ‘w’ or ‘b’ characters separated by spaces.
Limit:
+(1D)Testcase 1: H = 1, N = 1, M ≤ 4
+(1D)Testcase 2: H = 1, N = 1, M ≤ 16
*(1D)Testcase 3: H = 1, N = 1, M ≤ 20
+(2D)Testcase 4: H = 1, N ≤ 2, M ≤ 2
+(2D)Testcase 5: H = 1, N ≤ 4, M ≤ 4
*(2D)Testcase 6: H = 1, N ≤ 8, M ≤ 8
*(2D)Testcase 7: H = 1, N ≤ 15, M ≤ 15
+(3D)Testcase 8: H ≤ 2, N ≤ 2, M ≤ 2
+(3D)Testcase 9: H ≤ 2, N ≤ 3, M ≤ 3
*(3D)Testcase 10: H ≤ 2, N ≤ 4, M ≤ 4
*(3D)Testcase 11: H ≤ 3, N ≤ 4, M ≤ 4
*(3D)Testcase 12: H ≤ 4, N ≤ 4, M ≤ 4
Note:
The testcases marked "+" can be solved following the brute-force enumeration method used in the practice problem "13402 - Flip Game", while those marked "*" need a more efficient method!
Output
For each test, output the minimum number of rounds to reach a winning configuration, in case such a configuration exists, or the word "oops".
Sample Input Download
Sample Output Download
Tags
Discuss
Description
CHEAT SHEET
BASIC I/O
printf() and scanf() format
printf("%d", n);
scanf("%d", &n);
FORMAT ARGUMENT TYPE
%d, %i int decimal
%u unsigned int
%x unsigned int hexadecimal
%#x unsigned int hexadecimal with prefix 0x
%f double
%Lf long double
%e, %E double scientific notation
%c int to print a character
%s char * string (character array ended with '\0')
%p void * print memory address
%g, %G double %f or %e depending on the length
FORMAT ARGUMENT TYPE
%d int * &n, store the input integer in n
%ld long *
%lld long long *
%u unsigned int *
%f float * read float
%lf double * read double
%Lf long double * read long double
%c char * read 3 characters %3c
%s char * read a string until whitespace
%n int * with %s, to get string length
char a[100]; int len;
scanf("%s%n", a, &len);
len will have the string length
OPERATION
! && || == != + - * / %
> < >= <=
To create a 5-by-5 two-dimensional array, we need to write
int a[5][5];
It will be indexed as follows:
a[0][0] |
a[0][1] |
a[0][2] |
a[0][3] |
a[0][4] |
a[1][0] |
a[1][1] |
a[1][2] |
a[1][3] |
a[1][4] |
a[2][0] |
a[2][1] |
a[2][2] |
a[2][3] |
a[2][4] |
a[3][0] |
a[3][1] |
a[3][2] |
a[3][3] |
a[3][4] |
a[4][0] |
a[4][1] |
a[4][2] |
a[4][3] |
a[4][4] |
How to read the following data?
1 2 3 4 5 e
#include <stdio.h>
int main(void)
{
int x;
while (scanf("%d", &x) == 1) {
printf("x=%d\n", x);
}
return 0;
}
How to read the following data?
2
L 5 2
D 5 3
#include <stdio.h>
int main(void)
{
char ch;
int i, n, row, col;
scanf("%d", &n);
for (i=0; i<n; i++) {
while(getchar()!='\n');
scanf("%c%d%d", &ch, &row, &col);
}
return 0;
}
Using for loops to print a two-dimensional array
for(i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%5d", A[i][j]);
}
printf("\n");
}
Using bubble sort to rearrange an array ap
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-1-i; j++)
{
if (ap[j] > ap[j + 1])
{
temp = ap[j];
ap[j] = ap[j + 1];
ap[j + 1] = temp;
}
}
}
qsort
you have to include <stdlib.h>
usage :
void qsort (void *array, size_t count, size_t size, comparison_fn_t compare);
qsort an int array
int compare_int (const void *a, const void *b)
{
const int *va = (const int *) a;
const int *vb = (const int *) b;
return *va-*vb;
}
qsort a double array
int compare_double (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
How to avoid common errors and how to debug for OJ
1. Put the arrays in the 'global' area. Set their size bigger than required. Avoid using variable-length arrays (e.g. int arr[n];). Keep the array size fix (e.g., int arr[1000];).
2. After writing the code for reading input data, you may print out the data to check if your code reads them correctly. Do not proceed to write subsequent code before you confirm that.
3. If your program crashes, usually it is caused by memory-related errors. Check the ranges of for-loops to see if your code attempts to read or write the elements out of the arrays’ boundary.
*(a+i) is equivalent to a[i]
(a+i) is equivalent to &a[i]
FREQUENTLY USED FUNCTIONS
#include <string.h>
char str[10];
scanf("%s", str);
fgets (str, 10, stdin);
gets(str);
to get the string length : strlen(str)
to compare two strings : strcmp(str1, str2) ==0 if equal
to compare the first n chars of two strings : strncmp(str1, str2, n) ==0 if equal
to copy str2 to str1 : strcpy(str1, str2)
to copy the first n chars of str2 to str1 : strncpy(str1,str2, n), remember to add '\0' to str1
to get the first token in str1 splitted by delim : token = strtok(str1, delim), and use loop to get next token
example:
#include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.tutorialspoint.com - website"; const char s[2] = "-"; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while( token != NULL ) { printf( " %s\n", token ); token = strtok(NULL, s); } return(0); }
to check if str2 is str1's substring: strstr(str1, str2), return pointer to the first character of the substring in str1.
example:
#include <stdio.h> #include <string.h> int main () { const char haystack[20] = "TutorialsPoint"; const char needle[10] = "Point"; char *ret; ret = strstr(haystack, needle); printf("The substring is: %s\n", ret); return(0); }
#include <ctype.h>
isspace(ch), islower(ch), isupper(ch), isdigit(ch)
isalpha(ch), toupper(ch), tolower(ch)