当前位置:网站首页>leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]
leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]
2022-06-10 16:08:00 【Review of the white speed Dragon King】

analysis
use dp[x][i][j] It means that the beginning and the end are x Of s[i:j]( Contains two endpoints ) The number of different palindrome strings
Four situations :
1. if s[i] = s[j] = x: dp[x][i][j] = (2 + sum(d[i + 1][j - 1] for d in dp)) % MOD Add two yes x and xx
Get rid of any other characters that can start and end in the middle So it is sum
2. If only s[i] = x:dp[x][i][j] = dp[k][i][j - 1] Lose the last
3. If only s[j] = x:dp[x][i][j] = dp[k][i + 1][j] Lose the first
4. If not x:dp[k][i][j] = dp[k][i + 1][j - 1] Throw it all away
Then initially we can get each dp[x][i][i] All are 1 It looks like , Is a main diagonal
Because we are going to d[i + 1][j - 1] So it's equivalent to considering the next grid and the left grid
therefore , We need to traverse diagonally
Finally back to sum(d[0][n - 1] for d in dp) % MOD
ac code
class Solution:
def countPalindromicSubsequences(self, s: str) -> int:
# dp[c][i][j] It means that s[i:j] With c Number of different palindrome strings at the beginning and end
# From point to face
MOD = 10 ** 9 + 7
n = len(s)
# abcd
dp = [[[0] * n for _ in range(n)] for _ in range(4)]
# dp[c][i][i] = 1
for i, c in enumerate(s):
dp[ord(c) - ord('a')][i][i] = 1
for diff in range(1, n):
for j in range(diff, n):
i = j - diff
for k, c in enumerate("abcd"):
# xx and x => 2
if s[i] == c and s[j] == c:
dp[k][i][j] = (2 + sum(d[i + 1][j - 1] for d in dp)) % MOD
# drop s[j]
elif s[i] == c:
dp[k][i][j] = dp[k][i][j - 1]
# drop s[i]
elif s[j] == c:
dp[k][i][j] = dp[k][i + 1][j]
# drop s[i] + s[j]
else:
dp[k][i][j] = dp[k][i + 1][j - 1]
return sum(d[0][n - 1] for d in dp) % MOD
summary
Consider from point to surface
Spread one by one i and j Move out
So it's a diagonal calculation
Then the initial conditions are simple
There are four other shifts that can be understood
So the palindrome string can be considered by the interval of point and surface dp 了
The special thing here is to separate each letter as a dimension
边栏推荐
猜你喜欢

点击解锁广和通5G模组“关键词”
![2D human pose estimation with residual log likelihood estimation (RLE) [link only]](/img/c7/9c25da07236ef0bd241b6023e82306.gif)
2D human pose estimation with residual log likelihood estimation (RLE) [link only]

What has guanghetong done in the three years of 5g business from "seeding in the first generation" to "flower on the ground"?

服务器运维环境安全体系(下篇)

SQL语言

我用 MATLAB 复刻了抖音爆火小游戏 苹果蛇

RK3308--8声道改成双声道+录音增益

姿态估计之2D人体姿态估计 - SimDR: Is 2D Heatmap Representation Even Necessary for Human Pose Estimation?

智能电网终极Buff | 广和通模组贯穿“发、输、变、配、用”全环节

MapReduce之Reduce阶段的join操作案例
随机推荐
运行mapreduce任务缺失setJarByClass()报错找不到类
Distribution aware coordinate representation for human pose estimation
我用 MATLAB 复刻了抖音爆火小游戏 苹果蛇
LocalDate与Date相互转换
SQL language
姿态估计之2D人体姿态估计 - Distribution Aware Coordinate Representation for Human Pose Estimation【转-修改】
作用域和闭包
Unified certification center oauth2 certification pit
Sword finger offer 06 Print linked list from end to end
[section 7 function]
Docket command
Click to unlock "keyword" of guanghetong 5g module
测试用例常用方法和选择原则
广和通携手中国移动、惠普、联发科、英特尔合作打造5G全互联PC泛终端系列产品
Driver development and abnormal analysis of "technical dry goods" industrial touch screen (serial)
How to improve document management
uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
广和通高算力智能模组为万亿级市场5G C-V2X注智
【无标题】
“绽放杯”5G应用奖项大满贯!广和通多个联合项目荣获通用产品专题赛一、二、三等奖