当前位置:网站首页>动态规划入门二(5.647.62)
动态规划入门二(5.647.62)
2022-07-02 12:12:00 【-小小白-】
一.最长回文子串

1.构造数组dp[ len ][ len ],dp[ x ][ y ]表示字符串s从x到y这段字符是否是回文子串
2.赋初值,i从0到 len - 1让dp[ i ][ i ]都为1,因为单个字符也属于回文子串。
3.状态转移方程:从x到y这段字符串要是回文串的前提是s[ x ] == s[ y ],且x + 1到y - 1这段也得是回文串。因此有 dp[ x ][ y ] = (dp[x + 1][y - 1] && s[ x ] == s[ y ])。(y - x > 2)
注意:需要让遍历整个字符串可能的每一段(时间复杂度为n^2)。遍历时用maxlen记录最长的子串长度,beginindex记录最长字串出现的首字符位置。c语言没有切割字符串的函数,返回答案较麻烦。
char * longestPalindrome(char * s){
int maxlen = 1, beginindex = 0, i, j, len;
len = strlen(s);
if(len < 2){
return s;
}
int dp[len][len];
for(i = 0; i < len; i++){
dp[i][i] = true;
}
for(i = 0; i < len; i++){
for(j = 0; j <= i; j++){
if (s[i] != s[j]) {
dp[j][i] = false;
}
else {
if (i - j < 3) {
dp[j][i] = true;
}
else {
dp[j][i] = dp[j + 1][i - 1];
}
}
if (dp[j][i] && i - j + 1 > maxlen) {
maxlen = i - j + 1;
beginindex = j;
}
}
}
s[beginindex + maxlen] = '\0';
s = &s[beginindex];
return s;
}二.回文子串

此题与寻找最长回文子串相似,不同之处在于不用记录最长字串,需要用计数器count,每次找到一个回文串就count++即可
int countSubstrings(char * s){
int count = 0;
int len, i, j;
len = strlen(s);
int dp[len][len];
for(i = 0; i < len; i++){
dp[i][i] = 1;
}
for(i = 0; i < len; i++){
for(j = 0; j <= i; j++){
if(s[i] != s[j]){
dp[j][i] = 0;
}
else{
if(i - j <= 2){
dp[j][i] = 1;
}
else{
dp[j][i] = dp[j + 1][i - 1];
}
if(dp[j][i] == 1){
count++;
}
}
}
}
return count;
}三.不同路径

1.构造数组dp[ m ][ n ],dp[ x ][ y ]表示走到x,y位置有几种走法。
2.赋初值,让数组的第一行和第一列都等于1。
3.状态转移方程:容易得到dp[ x ][ y ] = dp[ x ][ y - 1] + dp[ x - 1][ y ]。
int uniquePaths(int m, int n) {
int dp[m][n];
for (int i = 0; i < m; ++i) {
dp[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
dp[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return f[m - 1][n - 1];
}注意这里dp[ i ][ j ]的值只与其上一行以及其同一行的前一个值有关,所以可以利用滚动数组,让空间复杂度从m*n降低到min(m, n)。
int uniquePaths(int m, int n){
int dp[n];
for(int i = 0; i < n; i++){
dp[i] = 1;
}
for(int i = 1; i < m; i++){
dp[0] = 1;
for(int j = 1; j < n; j++){
dp[j] = dp[j] + dp[j-1];
}
}
return dp[n-1];
}边栏推荐
- I made an istio workshop. This is the first introduction
- MySQL -- Index Optimization -- order by
- [leetcode] 167 - sum of two numbers II - enter an ordered array
- 夏季高考文化成绩一分一段表
- (4) Flink's table API and SQL table schema
- 高考分数线爬取
- List of sergeant schools
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
- Solve the problem of frequent interruption of mobaxterm remote connection
- 【LeetCode】344-反转字符串
猜你喜欢

03.golang初步使用

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

Pytoch saves tensor to Mat file

Custom exception

党史纪实主题公益数字文创产品正式上线

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData

. Solution to the problem of Chinese garbled code when net core reads files

18_ Redis_ Redis master-slave replication & cluster building

Bing.com網站
随机推荐
4. Jctree related knowledge learning
Custom exception
6090. 极大极小游戏
LeetCode刷题——验证二叉树的前序序列化#331#Medium
College entrance examination score line climbing
. Net again! Happy 20th birthday
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
LeetCode刷题——去除重复字母#316#Medium
SQL transaction
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
[leetcode] 977 - carré du tableau ordonné
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
士官类学校名录
Steps for Navicat to create a new database
15_ Redis_ Redis. Conf detailed explanation
提前批院校名称
PTA 天梯赛习题集 L2-001 城市间紧急救援
Deux séquences ergodiques connues pour construire des arbres binaires
Redux - detailed explanation
Leetcode skimming -- count the number of numbers with different numbers 357 medium