当前位置:网站首页>Introduction to Dynamic Planning II (5.647.62)
Introduction to Dynamic Planning II (5.647.62)
2022-07-02 15:49:00 【-Xiaoxiaobai-】
One . Longest text substring
1. Construct an array dp[ len ][ len ],dp[ x ][ y ] Representation string s from x To y Whether this character is a palindrome substring
2. Assign initial value to ,i from 0 To len - 1 Give Way dp[ i ][ i ] All for 1, Because a single character also belongs to palindrome substring .
3. State transition equation : from x To y If this string is a palindrome string, the premise is s[ x ] == s[ y ], And x + 1 To y - 1 This paragraph must also be a palindrome string . So there is dp[ x ][ y ] = (dp[x + 1][y - 1] && s[ x ] == s[ y ]).(y - x > 2)
Be careful : You need to traverse every possible segment of the entire string ( The time complexity is n^2). For traversal maxlen Record the longest substring length ,beginindex Record the first character position of the longest string .c Language has no function to cut strings , It is troublesome to return the answer .
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;
}
Two . Palindrome string
This question is similar to finding the longest palindrome substring , The difference is that the longest string is not recorded , Counter required count, Every time I find a palindrome string count++ that will do
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;
}
3、 ... and . Different paths
1. Construct an array dp[ m ][ n ],dp[ x ][ y ] Said go x,y There are several ways to move the position .
2. Assign initial value to , Let the first row and the first column of the array be equal to 1.
3. State transition equation : Easy to get 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];
}
Note that there dp[ i ][ j ] The value of is only related to its previous line and its previous value in the same line , So you can use a scrolling array , Let the space complexity change from m*n Down to 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];
}
边栏推荐
- /Bin/ld: cannot find -lssl
- [leetcode] 877 stone game
- Target detection - make your own deep learning target detection data set with labelimg
- [leetcode] 876 intermediate node of linked list
- Pyinstaller打包exe附带图片的方法
- [leetcode] 417 - Pacific Atlantic current problem
- 6091. 划分数组使最大差为 K
- XPT2046 四线电阻式触摸屏
- 【LeetCode】877-石子游戏
- 蚂蚁集团大规模图计算系统TuGraph通过国家级评测
猜你喜欢
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals
Wechat Alipay account system and payment interface business process
[network security] network asset collection
Basic knowledge of cryptography
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
动态规划入门一,队列的bfs(70.121.279.200)
PostgresSQL 流复制 主备切换 主库无读写宕机场景
Deux séquences ergodiques connues pour construire des arbres binaires
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
随机推荐
(Wanzi essence knowledge summary) basic knowledge of shell script programming
【idea】推荐一个idea翻译插件:Translation「建议收藏」
【Leetcode】167-两数之和II -输入有序数组
使用 percona 工具给 MySQL 表加字段中断后该如何操作
[leetcode] 1254 - count the number of closed Islands
2279. Maximum number of backpacks filled with stones
Target detection - make your own deep learning target detection data set with labelimg
[network security] network asset collection
2278. 字母在字符串中的百分比
Strings and arrays
已知两种遍历序列构造二叉树
6090. Minimax games
[leetcode] 200 number of islands
[leetcode] 344 reverse string
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
基于 Nebula Graph 构建百亿关系知识图谱实践
Moveit obstacle avoidance path planning demo
Pyinstaller's method of packaging pictures attached to exe
fastjson List转JSONArray以及JSONArray转List「建议收藏」
C # get PLC information (kepserver) II