Professional Documents
Culture Documents
BaiGiaiSS
BaiGiaiSS
0
/\
1 2
/\\
345
\
6
If the given level is larger than the maximum level of the tree, then print
"No such level" (without the quotes).
Sample Input:
Test 1: 2 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Test 2: 3 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Test 3: 10 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Sample Output:
In ra tổng tại một nút K nào đó (VD: K=2 thì in ra tổng tại nút 2)
Test 1: 12
Test 2: 6
Test 3: -1(nếu k không tồn tại tại nút đó in ra -1)
Thuật toán:
int b = Integer.parseInt(s1); //
char[] a = s2.toCharArray();
int leng = a.length;
int bac = -1;
for (int i = 0; i < leng; i++) {
if (a[i] == '(') {
bac++;
} else if (a[i] == ')') {
bac--;
} else {
int valueI = Integer.parseInt(a[i] + "");
data[bac] = data[bac] + valueI;
}
}
if (b > 0 && data[b] == 0) {
System.out.println("" + -1);
} else {
System.out.println("" + data[b]);
}
}
}
Bài 2:
//3
//(a+(b*c))
//((a+b)*(z+x))
//((a+t)*((b+(a+c))^(c+d)))
//
//Output:
//abc*+
//ab+zx+*
//at+bac++cd+^*
Thuật toán:
Nếu là dấu “)” thì lấy phần từ đầu in bằng method peek. Sau đó dung pop để lấy
ra khỏi stack
public class Main {
public static void main(String[] args) {
////DONE
Scanner mScanner = new Scanner(System.in);
int T = Integer.parseInt(mScanner.nextLine());
Stack<Character> mStack;
for (int i = 0; i < T; i++) {
mStack = new Stack<>();
String testCase = mScanner.nextLine();
char[] lengStr = testCase.toCharArray();
int size = lengStr.length;
for (int k = 0; k < size; k++) {
if (Character.isLetter(lengStr[k])) {
System.out.print(lengStr[k]);
} else if (lengStr[k] == ')') {
System.out.print(mStack.peek());
mStack.pop();
} else if (lengStr[k] != '(') {
mStack.push(lengStr[k]);
}
}
System.out.println();
}
}
}
Bài 3:
cho 1 ma trận cấp N với các giá trị cho trước không giống nhau.
tưởng tượng có một hòn bi, bạn để nó ở đỉnh cao thì sẽ lăn xuống nơi thấp hơn. coi độ cao của địa
hình tương đương giá trị của phần tử trong ma trận, và giả sử viên bi sẽ lăn từ điểm đặt tới điểm mà
thấp hơn nhất bên cạnh so với nó. (chỉ tính ngang dọc).
output : tìm quãng đường dài nhất mà viên bi có thể lăn được.
vd :
23 24 35 13 44 10
37 27 79 60 66 11
45 78 89 90 18 30
50 65 72 67 55 51
57 71 81 17 92 14
31 47 29 83 77 15
quãng đường dài nhất : 89 - 72 - 65 - 50 - 45 - 37 - 23 : 6 quãng đường.
Thuật toán:
Duyệt từng điểm của ma trận:
Tìm min của 4 đỉnh xung quanh điểm đang duyệt. lặp lại cho đến khi không tìm đc min.
Mỗi lần tìm đc min thì tăng biến đếm.
Với mỗi đỉnh thì kiểm tra xem giá trị tìm đc có lớn nhất không.
Cuối cùng in ra số lớn nhất:
public class Main2 {
static int[][] input;
static int n;
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, 1, -1 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
int value;
for (int testcase = 1; testcase <= tc; testcase++) {
int max = Integer.MIN_VALUE;
n = sc.nextInt();
input = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
input[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
value = duyet(i, j);
if (value > max)
max = value;
}
}
System.out.println("#" + testcase + " " + max);
}
}
private static int duyet(int i, int j) {
int mLength = 0;
int x2 = 0;
int min = Integer.MAX_VALUE;
int y2 = 0;
boolean check = false;
for (int u = 0; u < 4; u++) {
int x1 = i + dx[u];
int y1 = j + dy[u];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && input[x1][y1]
< input[i][j] && input[x1][y1] < min) {
min = input[x2][y2];
x2 = x1;
y2 = y1;
check = true;
}
}
if (check) {
mLength++;
mLength += duyet(x2, y2);
}
return mLength;
}
}
Bài 4:
Bruce Force's keyboard is broken, only a few keys are still working. Bruce has figured out he can
still type texts by switching the keyboard layout whenever he needs to type a letter which is
currently not mapped to any of the mworking keys of the keyboard.
You are given a text that Bruce wishes to type, and he asks you if you can tell him the maximum
number of consecutive characters in the text which can be typed without having to switch the
keyboard layout. For simplicity, we assume that each key of the keyboard will be mapped to
exactly one character, and it is not possible to type other characters by combination of different
keys. This means that Bruce wants to know the length of the largest substring of the text which
consists of at most m different characters.
Input
The input contains several test cases, each test case consisting of two lines. The first line of each
test case contains the number m (1 ≤ m ≤ 128), which specifies how many keys on the keyboard are
still working. The second line of each test case contains the text which Bruce wants to type. You
may assume that the length of this text does not exceed 1 million characters. Note that the input
may contain space characters, which should be handled like any other character.
The last test case is followed by a line containing one zero.
Output
For each test case, print one line with the length of the largest substring of the text which consists
of at most mdifferent characters.
Example
Input:
5
This can't be solved by brute force.
1
Mississippi
0
Output:
7
2
Thuật toán:
Tạo 1 biếm đếm để so sánh vs number key còn trên bàn phím
Duyệt từng chữ cái 1. Tạo newmax để check max của lần duyệt dó
Nếu chữ cái chưa có trong array thì add vào array và tăng đếm. tăng max
ở cả 2 trường hợp trên đều kiểm tra đem==number key thì break.
Sau khi break thì kiểm tra newmaxx>max thì gán max=newmax
Thuật toán:
Nếu gặp số 1 thì duyệt về 4 hướng cho tới khi gặp số 0. return min của 4 hướng
Sauk hi duyệt hết thì cộng tất cả các số return khi duyệt số 1
public class Main {
static int MT[][];
static int mWater;
static int mSize;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
mWater = 0;
mSize = sc.nextInt();
MT = new int[mSize][mSize];
for (int i = 0; i < mSize; i++) {
for (int j = 0; j < mSize; j++) {
MT[i][j] = sc.nextInt();
}
}
for (int i = 0; i < mSize; i++) {
for (int j = 0; j < mSize; j++) {
if (MT[i][j] == 1) {
mWater += check(i, j);
}
}
}
System.out.println("Output:"+mWater);
}
}
private static int check(int i, int j) {
int dsUp = 1, dsDown = 1, dsLeft = 1, dsRight = 1;
int iC = i, jC = j;
int min;
boolean checkR = false, checkL = false, checkU = false, checkD =
false;
while (iC > 0) {
checkU = true;
iC--;
if (MT[iC][j] == 1) {
dsUp++;
} else {
break;
}
}
iC = i;
while (iC + 1 < mSize) {
checkD = true;
iC++;
if (MT[iC][j] == 1) {
dsDown++;
} else {
break;
}
}
iC = i;
while (jC + 1 < mSize) {
checkR = true;
jC++;
if (MT[i][jC] == 1) {
dsRight++;
} else {
break;
}
}
jC=j;
while (jC > 0) {
checkL = true;
jC--;
if (MT[i][jC] == 1) {
dsLeft++;
} else {
break;
}
}
if (checkR && checkL && checkD && checkU) {
min = Math.min(Math.min(dsUp, dsDown), Math.min(dsRight,
dsLeft));
} else {
ArrayList<Integer> arrNumber = new ArrayList<>();
if (checkD) {
arrNumber.add(dsDown);
}
if (checkU) {
arrNumber.add(dsUp);
}
if (checkL) {
arrNumber.add(dsLeft);
}
if (checkR) {
arrNumber.add(dsRight);
}
if (arrNumber.size() == 1) {
min = arrNumber.get(0);
} else if (arrNumber.size() == 2) {
min = Math.min(arrNumber.get(0), arrNumber.get(1));
} else {
min = Math.min(arrNumber.get(0),
Math.min(arrNumber.get(1), arrNumber.get(2)));
}
}
return min;
}
}
Bài 6:
Tạo 1 mảng để lưu trữ ma trận và 1 mảng để lưu trữ xem nó đc duyệt chưa
Nếu gặp 1 thì tăng biến đếm số cụm, biến đếm số sao và gán vị trí mảng
visited=1 sau đó duyệt đệ quy về 4 hướng
Return dem:
Sau khi kết thúc hàm thì so sánh max có lớn hơn biến đếm số sao k. nếu k thì
gán max= đếm số sao
4 chữ cái được định nghĩa bởi 4 ma trận 3x3 (size chữ luôn là 3x3) như hình. Các chữ cái có thể xoay 90-
180-270 ví dụ như chữ T bên dưới
Các chữ cái trong 1 ma trận N x N ( N <= 100) thỏa mãn các điều kiện:
InPut:
Sample
0 0 0 0 0 0 0
1 0 1 0 0 0 0
1 1 1 0 0 0 0
1 0 1 0 1 0 0
0 0 0 0 1 0 0
0 0 0 0 1 1 1
0 0 0 0 0 0 0
Thuật toán:
Nếu gặp số 1 thì duyệt 9 điểm xung quanh xem nó là chữ gì. Vào chữ nào tăng
biến đếm chữ đó
}
Bài 8:
//Cho 1 xây ký tự kích thước N (4<= N <=128, N = 2^k). Hãy xây dựng 1 ma trân
đối xứng NxN với hàng đầu tiên là thứ tự của xâu đó.
//vd: input
//ABCD
//output:
//ABCD
//BADC
//CDAB
//DCBA
import java.util.Scanner;
public class MaTranDoiXung {
}
Bài 9:
Example
4 4
2 1 4 2 4
1 1 2
1 2 3
2 3
1 1 2
1 1 1
0
1000 1000
1 3 3
0
0
0 0
Output:
Board 1 has 6 safe squares.
Board 2 has 0 safe squares.
Board 3 has 996998 safe squares.
Thuật toán: Với ma trận [N][N] thì ta sẽ đánh dấu các quân cờ lên. Quân hậu=1
quân mã =2 quân tốt =3
Còn lại sẽ là 0
Duyệt bước đi của quân hậu. nếu ăn đc mà k bị chặn bởi tốt thì đánh 4
Duyệt bước đi của quân mã. Nếu ăn đc mà k bị chặn bởi tốt thì đánh 5
//Chỉnh sửa lại đoạn đi của mã xem nó có bị chặn của tốt k và submit lại code
vào trang http://www.spoj.com/problems/QKP/
import java.util.ArrayList;
import java.util.Scanner;
class Main {
static int[][] maTranCo;
static int[][] mSafe;
static int mRow;
static int mCol;
public static void main(String[] args) throws java.lang.Exception {
//FIXME Sai trên spoj http://www.spoj.com/problems/QKP/
try {
Scanner mScanner = new Scanner(System.in);
String sizeMaTran = mScanner.nextLine();
ArrayList<QuanCo> arrQ = new ArrayList<>();
ArrayList<QuanCo> arrK = new ArrayList<>();
ArrayList<TestQPK> arrTestcase = new ArrayList<>();
while (!sizeMaTran.equals("0 0")) {
String a = mScanner.nextLine();
String b = mScanner.nextLine();
String c = mScanner.nextLine();
arrTestcase.add(new TestQPK(sizeMaTran, a, b, c));
sizeMaTran = mScanner.nextLine();
}
for (int d = 0; d < arrTestcase.size(); d++) {
String sizeMT=arrTestcase.get(d).getA();
String strQ = arrTestcase.get(d).getB();
String strK = arrTestcase.get(d).getC();
String strP = arrTestcase.get(d).getD();
arrQ.clear();
arrK.clear();
String[] sizeMTArr = sizeMT.split(" ");
mRow = Integer.parseInt(sizeMTArr[0]);
mCol = Integer.parseInt(sizeMTArr[1]);
maTranCo = new int[mRow][mCol];
mSafe = new int[mRow][mCol];
int numberQ = Integer.parseInt(strQ.substring(0, 1));
int numberK = Integer.parseInt(strK.substring(0, 1));
int numberP = Integer.parseInt(strP.substring(0, 1));
if (numberQ != 0) {
String[] vitriQ = strQ.split(" ");
int j = 0;
for (int i = 0; i < numberQ; i++) {
// Q =1
maTranCo[Integer.parseInt(vitriQ[j + 1]) -
1][Integer.parseInt(vitriQ[j + 2]) - 1] = 1;
mSafe[Integer.parseInt(vitriQ[j + 1]) - 1]
[Integer.parseInt(vitriQ[j + 2]) - 1] = 1;
arrQ.add(new
QuanCo(Integer.parseInt(vitriQ[j + 1]) - 1, Integer.parseInt(vitriQ[j + 2]) -
1));
j += 2;
}
}
if (numberK != 0) {
String[] vitriK = strK.split(" ");
int j = 0;
for (int i = 0; i < numberK; i++) {
// K = 2
maTranCo[Integer.parseInt(vitriK[j + 1]) -
1][Integer.parseInt(vitriK[j + 2]) - 1] = 2;
mSafe[Integer.parseInt(vitriK[j + 1]) - 1]
[Integer.parseInt(vitriK[j + 2]) - 1] = 2;
arrK.add(new
QuanCo(Integer.parseInt(vitriK[j + 1]) - 1, Integer.parseInt(vitriK[j + 2]) -
1));
j += 2;
}
}
if (numberP != 0) {
int j = 0;
String[] vitriP = strP.split(" ");
for (int i = 0; i < numberP; i++) {
// P =3
maTranCo[Integer.parseInt(vitriP[j + 1]) -
1][Integer.parseInt(vitriP[j + 2]) - 1] = 3;
mSafe[Integer.parseInt(vitriP[j + 1]) - 1]
[Integer.parseInt(vitriP[j + 2]) - 1] = 3;
j += 2;
}
}
int sizeQ = arrQ.size();
int sizeK = arrK.size();
for (int i = 0; i < sizeQ; i++) {
checkQ(arrQ.get(i).getA(), arrQ.get(i).getB());
}
for (int i = 0; i < sizeK; i++) {
checkK(arrK.get(i).getA(), arrK.get(i).getB());
}
int dem = 0;
for (int u = 0; u < mRow; u++) {
for (int v = 0; v < mCol; v++) {
if (mSafe[u][v] == 0) {
dem++;
}
}
}
System.out.println("Board " + (d + 1) + " has " + dem
+ " safe squares.");
}
mScanner.close();
} catch (Exception e) {
}
}
Thuật toán:
Sắp xếp 2 ma trận giá trị và số số lớn hơn đứng trước lại theo thứ tự từ bé
tới lớn
Lặp lại bước trên nếu số lớn hơn đúng trước vẫn >=0
tg = number_taller[minPos];
number_taller[minPos] = number_taller[i];
number_taller[i] = tg ;
}
int pos = number_taller[i];
int dem = 0;
do {
if (array[dem] == 0) {
dem++;
pos--;
} else
dem++;
} while (pos >= 0);
array[dem - 1] = height[i];
}
System.out.println();
for (int m = 0; m < number; m++) {
System.out.print(array[m] + " ");
}
}
}
}
BROKEN - Broken Keyboard:http://www.spoj.com/problems/BROKEN/(Time limit)
PALIN - The Next Palindrome:http://www.spoj.com/problems/PALIN/
TOANDFRO - To and Fro:http://www.spoj.com/problems/TOANDFRO/
HTLU:https://www.facebook.com/groups/1169889303023249/1173469379331908/?
hc_location=ufi(Done)
QUE1 - Queue (Rookie):http://www.spoj.com/problems/QUE1/(done)
CANTON - Count on Cantor:http://www.spoj.com/problems/CANTON/(done)
Đếm số cụm và số max sao trong cụm(Done)
ONP - Transform the Expression:http://www.spoj.com/problems/ONP/(done)
Dat nuoc:https://www.facebook.com/groups/1169889303023249/permalink/1171065656238947/
(done)
QKP:http://www.spoj.com/problems/QKP/ (done nhưng chưa chạy đúng trên server)