9.12网易笔试Java
最终结果过了2道。
第一题: 模拟即可
参考代码:
package bishi;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class one {
static class Node{
int left = -1;
int right = -1;
boolean haveSon = false;
}
static int ans = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
Set<Integer> set = new HashSet<>();
Node[] root = new Node[m + 1];
for(int i = 1; i <= m; i++){
root[i] = new Node();
}
for(int i = 0; i < n; i++){
int index = sc.nextInt();
String str = sc.next();
int next = sc.nextInt();
if(str.equals("left")){
root[index].left = next;
root[index].haveSon = true;
} else {
root[index].right = next;
root[index].haveSon = true;
}
set.add(next);
}
int rootIndex = -1;
for(int i = 1; i <= m; i++){
if(set.contains(i) == false){
rootIndex = i;
break;
}
}
dfs(rootIndex, root);
System.out.println(ans);
}
public static void dfs(int root, Node[] nodes){
if(nodes[root].haveSon == false){
return ;
}
int left = nodes[root].left;
int right = nodes[root].right;
if(left != -1 && nodes[left].haveSon == false && right != -1 && nodes[right].haveSon == false){
ans++;
return ;
}
if(left != -1)
dfs(left, nodes);
if(right != -1)
dfs(right, nodes);
}
}
差分数组搞一下
package bishi;
import java.util.HashMap;
import java.util.Scanner;
public class two {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
HashMap<Character, Integer> map = new HashMap<>();
map.put('a', 0);map.put('b', 1);map.put('c', 2);map.put('x', 3);map.put('y', 4);map.put('z', 5);
int max = 0;
int len = str.length();
int[][] sum = new int[len + 2][10];
for(int i = 1; i <= len; i++){
char c = str.charAt(i - 1);
if(check(c)){
int index = map.get(c);
for(int j = 0; j < 6; j++){
if(index == j){
sum[i][j] = sum[i - 1][j] + 1;
} else {
sum[i][j] = sum[i - 1][j];
}
}
} else {
for(int j = 0; j < 6; j++){
sum[i][j] = sum[i - 1][j];
}
}
}
// for(int i = 0; i <= len; i++){
// System.out.println("==============i = " + i + "=============");
// for(int j = 0; j < 6; j++){
// System.out.print(sum[i][j]);
// }
// System.out.println();
// }
for(int i = 1; i <= len; i++){
for(int j = i + max; j <= len; j++){
boolean flag = true;
for(int k = 0; k < 6; k++){
int val = sum[j][k] - sum[i - 1][k];
if((val & 1) == 1){
flag = false;
break;
}
}
if(flag){
max = Math.max(max, j - i + 1);
}
}
}
System.out.println(max);
}
public static boolean check(char c){
return c == 'a' || c == 'b' || c == 'c' || c == 'x' || c == 'y' || c == 'z';
}
}
第三题直接没做
第四题知道是二分图匹配 搞了好久好久 还是有问题 直接gg

