9.8 Xiaomi Qiuzhao Written Test Software Development Direction Programming Questions
first question
Time limit: 3000MS
Memory limit: 589824KB
Title description: Given two strings str1 and str2, output the length of the longest common subsequence of the two strings. Returns "0" if the longest common subsequence is empty. Currently given data, there will only be one longest common subsequence
input example
1A2C3D4E56
A1B2345C6D
output example
6
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int[][] dp = new int[str1.length() + 1][str2.length() + 1];
for (int i = 1; i <= str1.length(); i++) {
for (int j = 1; j <= str2.length(); j++) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
System.out.println(dp[str1.length()][str2.length()]);
}
}
}
second question
Time limit: 3000MS
Memory limit: 589824KB
Title description: You are given a sequence of 10 blocks consisting of only red, white, and blue colors. Now you need to arrange these blocks in the order of red, white, and blue. 1 can be used to represent red, 2 to represent white, and 3 to represent blue, and the required time complexity is O(n).
For example, given the color bar sequence as: {blue, white, red, white, blue, red, white, white, red, blue}, the permutation result is required to be: red, red, red, white, white, white, white , blue, blue, blue}
Input example:
3 2 1 2 3 1 2 2 1 3
output example
1 1 1 2 2 2 2 3 3 3
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
//Arrays.sort(a);
int[] sort = quickSort(a, 0, a.length - 1);
for (int j : sort) {
System.out.print(j + " ");
}
}
public static int[] quickSort(int[] array, int L, int R) {
if (L >= R)
return array;
//定义子串的左下标和右下标
int left = L;
int right = R;
//得到基数
int pivot = array[left];
while (left < right) {
//左边的下标小右边的下标且 基数右边的值大于基数
while (left < right && array[right] >= pivot) {
right--;
}
if (left < right) {
array[left] = array[right];
}
while (left < right && array[left] <= pivot) {
left++;
}
if (left < right) {
array[right] = array[left];
}
//如果两个下标相等那么将基数赋值在相等的下标的位置
if (left >= right) {
array[left] = pivot;
}
}
//分配左右子串遍历
//遍历左子串
quickSort(array, L, right - 1);
//遍历右子串
quickSort(array, right + 1, R);
return array;
}
}