| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11739 | cheat_sheet (mid2) |
|
| 12550 | Array Sorting(for mid2) |
|
| 12552 | Simple Pattern Matching of Substrings(for mid2) |
|
Description
printf() and scanf() format
printf("%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
scanf("%d", &n);
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
Frequently used functions
#include <string.h>
char str[10];
scanf("%s", str);
to get the string length using 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 append a copy of str2 to str1 strcat(str1, str2)
to copy the first n chars of str2 to str1 strncpy(str1,str2, n) remember to add '\0' to str1
#include <ctype.h>
isspace(ch), islower(ch), isupper(ch), isdigit(ch)
isalpha(ch), toupper(ch), tolower(ch)
To create a 5-by-5 two-dimensional array, we need to write
int a[5][5];
It will be indexed as follows:

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;
}
}
}
operators:
! && || == != + - * / %
> < >= <=
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]
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a two-dimensional array of size R x 5 (1 < R < 100).
We want to sort the two dimensional array according to each row.
For example:
|
5 |
1 |
3 |
11 |
25 |
|
45 |
82 |
97 |
73 |
63 |
|
13 |
47 |
34 |
26 |
14 |
After sorted
|
1 |
3 |
5 |
11 |
25 |
|
45 |
63 |
73 |
82 |
97 |
|
13 |
14 |
26 |
34 |
47 |
Note that
1. This problem involves three files.
- function.h: Function definition of sortArray.
- function.c: Function implementation of sortArray.
- 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 : bubble sort algorithm
/* Using bubble sort to rearrange an array A[n] */
for (i = n; i > 0; i--) {
for (j = 1; j < i; j++) {
if (A[j-1] > A[j]) {
/* swap A[j-1] A[j] */
}
}
}
function.h
main.c
Input
The first line has an integer N(1<=N<=5000), which means the number of test cases.
For each case, the first line has an integer R (1<R<100) represent the numbers of rows. The following R lines, each containing 5 integers, specify the elements of the two-dimensional array.
Output
Print out all elements of the sorted array row-by-row.
All of the integers in the same line are separated by a space and there is a '\n' at the end of each line. All of the arrays should be separated by a new line character (\n).
Sample Input Download
Sample Output Download
Partial Judge Code
12550.cPartial Judge Header
12550.hTags
Discuss
Description
A substring S' of a given string S with length N is a contiguous sequence of N letters from S.
For instance, the substrings with length 2 of “abcde” are “ab”, “bc”, “cd”, “de”.
Given a string S and an integer N together with another string P.
Your task is to find all substrings from S with length N that have the same pattern as P, and the frequencies of these substrings.
In this problem, the patterns of two strings A, B are the same if and only if the numbers of each kind of letters in A, B are equal.
For example:
-
A = “ouo”, B = “oou” have the same pattern
number of letter a~n o p~t u v~z A = “ouo” 0 2 0 1 0 B = “oou” 0 2 0 1 0 -
A = “pap”, B = “oao” have different patterns
number of letter a b~n o p q~z A = “pap” 1 0 0 2 0 B = “oao” 1 0 2 0 0
Moreover, under a given N, the frequency of a substring S' is its number of occurrences within all resulting substrings of S.
For example: S = “ababa”, S' = “aba”
- all substrings with length 3 = {“aba”, “bab”, “aba”}
- frequency of S' = 2
Hint:
To efficiently check if a substring S' and P have the same pattern as required by this problem:
- use two 1D arrays to record the numbers of each kind of letters in S' and P, respectively;
- Just like the example above. TP[0] = the number of ‘a’ in P, TP[1] = the number of ‘b’ in P, and so on
- check if each pair of corresponding elements in the two 1D arrays are equal.
Input
One string S and an integer N on the first line.
Another string P on the second line.
- 1 ≤ N ≤ 3,000
- 1 ≤ |S|, |P| ≤ 3,000
- S, P contain only lower-case English letters.
Output
Print on the first line the number M of distinct substrings of S with length N that have the same pattern as P.
On the following M lines, print the (S', F') pair for the M substrings that have the same pattern as P, where F' is the frequency of S', one pair per line, in decreasing order of their frequencies. When the frequencies tie, please use the lexicographical order of the substrings.
Remember ‘\n’ on the end of line.