| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11110 | cheat_sheet |
|
| 12073 | character replacement |
|
| 12945 | Magicka Spell |
|
| 13036 | Let's regular expression matching |
|
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
%c char to print a character
%s char * string (character array ended with '\0')
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)
#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:
|
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");
}
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.
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
There will be some sample code for this problem .
The first line contains a string s.
There will be only lowercase Latin characters in s.
The second line will be an integer T, which is the change time. And T< 107.
For the next T line , each line will contains two character x, y , which means for any x in s should be replacement as y .
- function.h: Function definition of changeCharacter.
- function.c: Function describe of changeCharacter.
- main.c: A driver program to test your implementation.
Input
| s | < 10000
x, y will be lowercase Latin characters
For the testcase one , there is no change to original string s.
If you get any Runtime error in testcase one, think twice what's wrong with your pointer :D .
Output
In the end , output the string s one time !
'\n' at the end of output.Sample Input Download
Sample Output Download
Partial Judge Code
12073.cPartial Judge Header
12073.hTags
Discuss
Description
There’re N magicka spells. Each spell is composed by several digits 1~9.
123, 7 are valid spells, but a12, 0, 073 are not.
The damage of the spell is the decimal number represented by itself.
We want to get largest damage by sort the digits of spell from large to small.
Let’s call the sorted spell as critical spell.
For example, 7731 is the critical spell of 1377, 7317, 3717, … .
Your task is to find all kinds of critical spells of N spells, and output them without repetition in decreasing order by their damages.
Hint
You may consider the following algorithm:
- Read N Spells as strings
- For each si, sort the digits of it to obtain its critical spell.
- Sort critical spells by their damages.
You can compare the length first.
If not same, output their relation. If equal, compare their digits. - Output all kinds of critical spells without repeat.
You should check whether each critical spell has been printed or not.
If not, print it.
For sorting, you can use the following code:
char s[30];
…
int len = strlen(s);
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (s[j] < s[j + 1]) {
/*swap s[j] and s[j+1]*/
}
}
}
|
Testcases
Testcase 1 ~ 5: |si| = |sj| for all i, j. |si| ≤ 8, for all i
Testcase 6 ~ 8: |si| ≠ |sj| for some i, j. |si| ≤ 8, for all i
Testcase 9 ~ 10: |si| ≠ |sj| for some i, j. |si| ≤ 50, for all i
Input
There’s N on the first line.
There’s a spell si on the each of following N lines.
It’s guaranteed that:
- 1 ≤ N ≤ 100
- 1 ≤ |si| ≤ 50
- Each charactor of si ∈ ‘1’~‘9’
Output
Print the critical spells without repetition in decreasing order on each line.
Remember ‘\n’ on the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
A regular expression is a sequence of characters that define a pattern of string.
In this problem, a regular expression pattern contains lowercase letters, character '*' and character '.', where:
- '.' matches any single character (for example, "a.b" matches "aab", "abb", "acb", ..., and "azb")
- '*' indicates 0 or more occurrences of the preceding character (for example, "ab*c" matches "ac", "abc", "abbc", "abbbc", and so on, that is, in this example, the preceding character b of * can occur 0 or more times)
A string and the regular expression is said to be matched if the entire string can be covered by the entire pattern.
Given a string S and a regular expression pattern P, and Q questions,
for each question, given 2 integers QS, QP,
answer if S[0, QS) matches the pattern P[0, QP).
S[0, QS) is a substring of string S from index 0 to index QS - 1, where the index starts from 0.
P[0, QP) is a substring of pattern P from index 0 to index QP - 1, where the index starts from 0.
Note that S[0, 0) is an empty string and P[0, 0) is an empty pattern.
ouo.
For #Sample Input 1:
0 0: Empty pattern matches empty string.
1 1: True since pattern "a" matches string "a".
2 3: False since the entire pattern "aaa" does not match string "aa".
4 3: False since pattern "aaa" does not match the entire string "aaaa".
For #Sample Input 2:
1 1: "a" matches "a".
1 2: "a*" matches "a", since "a*" matches 0 or many 'a'.
3 2: "a*" does not match "aab" since "a*" matches 0 or many 'a'.
For #Sample Input 3:
0 2: ".*" matches 0 of the preceding character '.' so matches emtpy string.
3 2: ".*" matches "aab" since ".*" matches 0 or many of the preceding element '.', and '.' match any single character.
3 4: ".*bc" doest not match "aab" since the last 'c' cannot match the given string.
3 5: ".*bc*" matches "aab" since "c*" matches 0 of the preceding character 'c', then pattern ".*b" matches "aab".
Input
The first line contains a string S,
the second line contains a regular expression pattern P,
and the third line contains an integer Q, representing that there are Q questions.
In the following Q lines, each line contains 2 integers QS, QP,
representing the question is "Does S[0, QS) match the pattern P[0, QP) ?".
For all testcases:
1 <= |S|, |P| <= 500,
1 <= Q <= 250000,
0 <= QS <= |S|,
0 <= QP <= |P|.
S contains only lowercase letters.
P contains only lowercase letters, character '.' or character '*'.
For test cases 1 ~ 2: P contains no character '*'.
For test cases 3 ~ 6: 1 <= |S|, |P| <= 20.
For test cases 7 ~ 8: P contains only '.' and '*'.
For test cases 9 ~ 10: Q <= 10.
Output
The output should contain Q lines.
For each question, output "True" if S[0, QS) matches P[0, QP) and "False" if not. (Without quotes)