## 蓝桥杯备赛第五篇（动态规划）

news/2024/4/24 17:19:55/文章来源:https://blog.csdn.net/m0_73473352/article/details/136434130

#### 1.数位dp

``````public class Main {static long[] limit;static int length;static long[][] dp;public static long dfs(int pos, int pre, boolean flag, boolean lead) {if (pos == length) return 1;if (!flag && !lead && dp[pos][pre] != -1) return dp[pos][pre];long max = (flag ? limit[pos] : 进制数);long sum = 0;for (int i = 0; i <= max; i++) {if (条件) {sum += dfs(pos + 1, i, flag && i == limit[pos], lead && i == 0);}}if (!flag && !lead) dp[pos][pre] = sum;return sum;}public static long solve(long num) {if (num == 0) return 1;length = ("" + num).length();limit = new long[length];dp = new long[length][10];for (int i = 0; i < length; i++) {Arrays.fill(dp[i], -1);}for (int i = length - 1; i >= 0; i--) {limit[i] = num % 进制数;num /= 进制数;}return dfs(0, 0, true, true);}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int a = scanner.nextInt();int b = scanner.nextInt();System.out.println(solve(b) - solve(a - 1));}
}``````

#### 3.最长公共子序列

``````public class Main {public static void main(String[] args) {char[] str1 = "abcdefgh".toCharArray();char[] str2 = "acjlfabhh".toCharArray();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[i - 1] == str2[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]);}
}
``````

#### 4.最大连续子序列和

``````public class Main {public static void main(String[] args) {int[] arr = new int[]{-2, 11, -4, 13, -5, -2};int[] dp = new int[arr.length];dp[0] = arr[0];int max = arr[0];for (int i = 1; i < arr.length; i++) {dp[i] = Math.max(dp[i - 1] + arr[i], arr[i]);max = Math.max(max, dp[i]);}System.out.println(max);}
}
``````

#### 5.最长上升子序列

``````public class Main {public static void main(String[] args) {int[] arr = new int[]{2, 1, 5, 3, 6, 4, 6, 3};int[] dp = new int[arr.length];dp[0] = 1;int max = dp[0];for (int i = 1; i < arr.length; i++) {for (int j = i - 1; j >= 0; j--) {if (arr[i] > arr[j]) {dp[i] = Math.max(dp[i], dp[j] + 1);}max = Math.max(max, dp[i]);}}System.out.println(max);}
}
``````

### Go 互斥锁的实现原理？

Go sync包提供了两种锁类型&#xff1a;互斥锁sync.Mutex 和 读写互斥锁sync.RWMutex&#xff0c;都属于悲观锁。 概念 Mutex是互斥锁&#xff0c;当一个 goroutine 获得了锁后&#xff0c;其他 goroutine 不能获取锁&#xff08;只能存在一个写者或读者&#xff0c;不能同时…

### 2024年腾讯云学生服务器优惠活动「云+校园」政策解读

2024年腾讯云学生服务器优惠活动「云校园」&#xff0c;学生服务器优惠价格&#xff1a;轻量应用服务器2核2G学生价30元3个月、58元6个月、112元一年&#xff0c;轻量应用服务器4核8G配置191.1元3个月、352.8元6个月、646.8元一年&#xff0c;CVM云服务器2核4G配置842.4元一年&…

### 力扣--从前序与中序遍历序列构造二叉树

torchvision的datasets 使用torchvision提供的数据集API&#xff0c;比较方便&#xff0c;如果在pycharm中下载很慢&#xff0c;可以URL链接到迅雷中进行下载&#xff08;有些URL链接在源码里&#xff09;代码如下&#xff1a; import torchvision # 导入 torchvision 库 # …