按层打印二叉树(中序和前序)

https://exercise.acmcoder.com/online/online_judge_ques?ques_id=4172&konwledgeId=169
image.png

  1. package other;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5. public class Main4 {
  6. public static void main(String[] args) {
  7. //452 3 1后序 425 1 3中序
  8. // 1 245 3 前序 425 1 3 i-inl
  9. Scanner input = new Scanner(System.in);
  10. int num = input.nextInt();
  11. int[] pre = new int[num];
  12. int[] in = new int[num];
  13. for (int i = 0; i < num; i++) {
  14. pre[i] = input.nextInt();
  15. }
  16. for (int i = 0; i < num; i++) {
  17. in[i] = input.nextInt();
  18. }
  19. Node head = createTree(pre,0,pre.length-1,in,0,in.length-1);
  20. printTree(head,num);
  21. }
  22. public static int findRoot(int root,int[] in,int il,int ir){
  23. for (int i = il; i <= ir; i++) {
  24. if(root == in[i]){
  25. return i;
  26. }
  27. }
  28. return -1;
  29. }
  30. public static void printTree(Node head,int num){
  31. Queue<Node> q = new LinkedList<>();
  32. q.add(head);
  33. int i = 0;
  34. while(!q.isEmpty()){
  35. Node n = q.poll();
  36. i++;
  37. System.out.print(n.val);
  38. if(i<num){
  39. System.out.print(" ");
  40. }
  41. if(n.left!=null){
  42. q.add(n.left);
  43. }
  44. if(n.right!=null){
  45. q.add(n.right);
  46. }
  47. }
  48. }
  49. public static Node createTree(int[] pre,int pl,int pr,int[] in,int il,int ir){
  50. if(pl>pr||il>ir){
  51. return null;
  52. }
  53. Node node = new Node();
  54. node.val = pre[pl];
  55. int i = findRoot(pre[pl],in,il,ir);
  56. node.left = createTree(pre,pl+1,pl+i-il,in,il,i-1);
  57. node.right = createTree(pre,pl+i-il+1,pr,in,i+1,ir);
  58. return node;
  59. }
  60. private static class Node{
  61. public int val;
  62. public Node left;
  63. public Node right;
  64. }
  65. }

utf8编码器 业务题

image.png

  1. package other;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. public class Main3 {
  6. public static void main(String[] args) {
  7. Scanner in = new Scanner(System.in);
  8. while(in.hasNext()){
  9. String str = in.nextLine();
  10. String[] arr = str.split("\\s+");
  11. int len = arr.length;
  12. int[] nums = new int[len];
  13. for(int i = 0; i < len; ++i){
  14. nums[i] = Integer.parseInt(arr[i]);
  15. }
  16. int i = 0;
  17. List<Integer> ans = new ArrayList<>();
  18. boolean isOk = true;
  19. while(i<len && isOk){
  20. int temp = 0;
  21. if(nums[i]<128){
  22. //0xxxxxxx
  23. ans.add(nums[i++]);
  24. }else if(nums[i]<224){
  25. //110xxxxx 10xxxxxx
  26. for (int j = 0; j < 2; j++) {
  27. if(j==0){
  28. nums[i]-=192;
  29. }else if(i<len && nums[i]>=128&&nums[i]<192){
  30. nums[i]-=128;
  31. }else{
  32. isOk =false;
  33. }
  34. if(isOk){
  35. temp += nums[i++]*Math.pow(2,(1-j)*6);
  36. }
  37. }
  38. ans.add(temp);
  39. }else if(nums[i]<240){
  40. //1110
  41. for (int j = 0; j < 3; j++) {
  42. if(j==0){
  43. nums[i]-=224;
  44. }else if(i<len && nums[i]>=128&&nums[i]<192){
  45. nums[i]-=128;
  46. }else{
  47. isOk =false;
  48. }
  49. if(isOk){
  50. temp += nums[i++]*Math.pow(2,(2-j)*6);
  51. }
  52. }
  53. ans.add(temp);
  54. }else if(nums[i]<248){
  55. //11110
  56. for (int j = 0; j < 4; j++) {
  57. if(j==0){
  58. nums[i]-=240;
  59. }else if(i<len && nums[i]>=128&&nums[i]<192){
  60. nums[i]-=128;
  61. }else{
  62. isOk =false;
  63. }
  64. if(isOk){
  65. temp += nums[i++]*Math.pow(2,(3-j)*6);
  66. }
  67. }
  68. ans.add(temp);
  69. }else if(nums[i]<252){
  70. //111110
  71. for (int j = 0; j < 5; j++) {
  72. if(j==0){
  73. nums[i]-=248;
  74. }else if(i<len && nums[i]>=128&&nums[i]<192){
  75. nums[i]-=128;
  76. }else{
  77. isOk =false;
  78. }
  79. if(isOk){
  80. temp += nums[i++]*Math.pow(2,(4-j)*6);
  81. }
  82. }
  83. ans.add(temp);
  84. }else if(nums[i]<254){
  85. //1111110
  86. for (int j = 0; j < 6; j++) {
  87. if(j==0){
  88. nums[i]-=252;
  89. }else if(i<len && nums[i]>=128&&nums[i]<192){
  90. nums[i]-=128;
  91. }else{
  92. isOk =false;
  93. }
  94. if(isOk){
  95. temp += nums[i++]*Math.pow(2,(5-j)*6);
  96. }
  97. }
  98. ans.add(temp);
  99. }else{
  100. isOk=false;
  101. break;
  102. }
  103. }
  104. if(isOk){
  105. for (int j = 0; j < ans.size() ; j++) {
  106. System.out.print(ans.get(j)+" ");
  107. }
  108. }else{
  109. System.out.print("no");
  110. }
  111. System.out.println();
  112. }
  113. }
  114. }

IP过滤器 前缀树

https://www.nowcoder.com/questionTerminal/8389e1ccd47d40ba859c2497a428d0ca
image.png

自己做的前缀树法

  1. package mypractice;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. public class Main2 {
  6. public static void main(String[] args) {
  7. Scanner in = new Scanner(System.in);
  8. int N = in.nextInt();
  9. int M = in.nextInt();
  10. in.nextLine();
  11. Node head = new Node(new HashMap<String,Node>());
  12. for (int i = 0; i < N; i++) {
  13. String s = in.nextLine();
  14. String[] rule = s.split("\\.");
  15. Node cur = head;
  16. for (int j = 0; j < rule.length ; j++) {
  17. if(!cur.children.containsKey(rule[j])){
  18. cur.children.put(rule[j],new Node(new HashMap<>()));
  19. }
  20. cur = cur.children.get(rule[j]);
  21. }
  22. cur.isEnd=true;
  23. }
  24. for(int i=0;i<M;i++){
  25. String s = in.nextLine();
  26. String[] ip = s.split("\\.");
  27. boolean res = search(head,ip);
  28. System.out.print((res ? 1 : 0)+" ");
  29. }
  30. }
  31. private static boolean search(Node head,String[] ip){
  32. Node cur = head;
  33. if(cur.children.containsKey(ip[0])){
  34. cur = cur.children.get(ip[0]);
  35. for (int i = 1; i < ip.length ; i++) {
  36. if(cur.children.containsKey(ip[i])){
  37. cur = cur.children.get(ip[i]);
  38. }else if(cur.children.containsKey("*")){
  39. return true;
  40. }else{
  41. break;
  42. }
  43. }
  44. if(cur.isEnd)return true;
  45. }
  46. cur = head;
  47. if(cur.children.containsKey("*")){
  48. cur = cur.children.get("*");
  49. // ip地址的第一个 第二个 第三个都可以用*去匹配
  50. for (int i = 3; i >= 1 ; i--) {
  51. boolean res = help(i,ip,cur);
  52. if(res)return true;
  53. }
  54. }
  55. return false;
  56. }
  57. private static boolean help(int i,String[] ip,Node cur){
  58. for (; i <ip.length ; i++) {
  59. if(cur.children.containsKey(ip[i])){
  60. cur = cur.children.get(ip[i]);
  61. }
  62. }
  63. return cur.isEnd;
  64. }
  65. private static class Node{
  66. public boolean isEnd;
  67. public Map<String,Node> children;
  68. public Node(Map<String, Node> children) {
  69. isEnd = false;
  70. this.children = children;
  71. }
  72. }
  73. }

暴力法

/**
 * 前缀 后缀 的匹配问题 easy
 */

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int M = sc.nextInt();

        sc.nextLine();

        //读取规则
        String[] patterns = new String[N];
        for(int i =0 ;i < N;i++){
            patterns[i] = sc.nextLine();
        }

        //读取IP
        String[] IPs = new String[M];
        for(int i = 0;i < M; i++){
            IPs[i] = sc.nextLine();
        }

        //暴力匹配
        for(int i = 0; i < IPs.length;i++){
            boolean lock = false;
            for(int j = 0; j < patterns.length;j++){
                String t = "";
                if(patterns[j].charAt(0) == '*'){
                    t = patterns[j].replace("*","");
                    if(IPs[i].endsWith(t)){
                        System.out.print(1 + " ");
                        lock = true;
                        break;
                    }
                }else if(patterns[j].charAt(patterns[j].length()-1) == '*'){
                    t = patterns[j].replace("*","");
                    if(IPs[i].startsWith(t)){
                        System.out.print(1 + " ");
                        lock = true;
                        break;
                    }
                }else{

                    if(patterns[j].equals(IPs[i])){
                        System.out.print(1 + " ");
                        lock = true;
                        break;
                    }
                }
            }
            if(lock == false){
                System.out.print(0 + " ");
            }
        }
    }
}

排队问题 动态规划

image.png
https://www.nowcoder.com/questionTerminal/5b36556ae602486f96be163c1ee21f1f

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] score = new int[n];
        for (int i = 0; i < score.length; i++) {
            score[i] = in.nextInt();
        }
        int k = in.nextInt();
        int d = in.nextInt();
        int[][] maxdp = new int[k][n];
        int[][] mindp = new int[k][n];
        //j位置结束 选取i+1个人能力最大值   i:0~k-1
        for (int i = 0; i < n; i++) {
            maxdp[0][i] = score[i];
            mindp[0][i] = score[i];
        }
        int max = Integer.MIN_VALUE;
        for (int i = 1; i < k; i++) {
            for (int j = 0; j < n; j++) {
                for (int p = j-1; p >=Math.max(0,j-d) ; p--) {
                    maxdp[i][j] = Math.max(maxdp[i][j],maxdp[i-1][p]*score[j]);
                    maxdp[i][j] = Math.max(maxdp[i][j],mindp[i-1][p]*score[j]);
                    mindp[i][j] = Math.min(mindp[i][j],mindp[i-1][p]*score[j]);
                    mindp[i][j] = Math.min(mindp[i][j],maxdp[i-1][p]*score[j]);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            max = Math.max(maxdp[k-1][i],max);
        }
        System.out.println(max);
    }
}