当前位置:网站首页>剑指 Offer 19. 正则表达式匹配
剑指 Offer 19. 正则表达式匹配
2022-07-01 00:32:00 【anieoo】
原题链接:剑指 Offer 19. 正则表达式匹配
solution:
状态表示:dp[i][j]表示s的前i个字符能否和p的前j个字符匹配
转移方程:
如果s[i] == p[j]表示s的第i个字符和p的第j个字符相同,因此dp[i][j] = dp[i - 1][j - 1].
当p[j] == '.'的时候,由于'.'可以表示任何字符所以dp[i][j] = dp[i - 1][j - 1].
当 p(j) == '*' 时,若 j>=2,dp[i][j] 可以从dp[i][j - 2] 转移,表示丢弃这一次的 '*' 和它之前的那个字符;若 i>0 且 s[i] == p[j - 1],表示这个字符可以利用这个 '*',则可以从dp[i - 1][j] 转移,表示利用 '*'。
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(),n = p.size();
s = " " + s;
p = " " + p;
//dp[i][j]表示s的前i个字符和p的前j个字符能否匹配
vector<vector<bool>> dp(m + 1, vector<bool> (n + 1, false));
dp[0][0] = true;
for(int i = 0;i <= m;i++)
for(int j = 1;j <= n;j++) {
if(i > 0 && (s[i] == p[j] | p[j] == '.'))
dp[i][j] = dp[i][j] | dp[i - 1][j - 1];
if(p[j] == '*') {
if(j >= 2) {
dp[i][j] = dp[i][j] | dp[i][j - 2];
if (i > 0 && (s[i] == p[j - 1] || p[j - 1] == '.'))
dp[i][j] = dp[i][j] | dp[i - 1][j];
}
}
}
return dp[m][n];
}
};
边栏推荐
- Excuse me, does Flink support synchronizing data to sqlserver
- Summer Challenge [FFH] harmonyos mobile phone remote control Dayu development board camera
- Gateway service gateway
- 2022-2028 global carbon fiber room scraper system industry research and trend analysis report
- The principle and related problems of acid in MySQL
- Redis - sentinel mode
- 2022-2028 global ICT test probe industry research and trend analysis report
- Longest valid bracket
- Search rotation sort array
- MySQL variables, stored procedures and functions
猜你喜欢
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
2022-2028 global rampant travel industry research and trend analysis report
[UML] UML class diagram
2022-2028 global PTFE lined valve industry research and trend analysis report
Pytorch auto derivation
Basic knowledge of Embedded Network - introduction of mqtt
Bridge emqx cloud data to AWS IOT through the public network
Implementation of OSD on Hisilicon platform (1)
Development of wireless U-shaped ultrasonic electric toothbrush
Stack frame
随机推荐
Red hat will apply container load server on project atomic
Why did kubernetes win? The changes in the container circle!
CSDN常用复杂公式模板记录
20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
Rust book materials - yazhijia Library
Redis - how to understand publishing and subscribing
Member management applet actual development 07 page Jump
Random ball size, random motion collision
The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
2022-2028 global PTFE lined valve industry research and trend analysis report
Luogu p1144 shortest circuit count
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
连表查询 select 生成
Pycharm useful shortcut keys
Thoughts on the future of data analysis in "miscellaneous talk"
Cloud security daily 220630: the IBM data protection platform has found an arbitrary code execution vulnerability, which needs to be upgraded as soon as possible
Implementation of OSD on Hisilicon platform (1)
What SQL statements are supported for data filtering
Self examination before school starts
lvm-snapshot:基于LVM快照的备份