当前位置:网站首页>Sword finger offer 19 Regular Expression Matching
Sword finger offer 19 Regular Expression Matching
2022-07-01 00:40:00 【anieoo】
Original link : The finger of the sword Offer 19. Regular Expression Matching
solution:
State means :dp[i][j] Express s Before i Can a character be the same as p Before j Characters match
Transfer equation :
If s[i] == p[j] Express s Of the i Characters and p Of the j Two characters are the same , therefore dp[i][j] = dp[i - 1][j - 1].
When p[j] == '.' When , because '.' Can represent any character, so dp[i][j] = dp[i - 1][j - 1].
When p(j) == '*' when , if j>=2,dp[i][j] It can be downloaded from dp[i][j - 2] Transfer , It means to discard this time '*' And the character before it ; if i>0 And s[i] == p[j - 1], Indicates that this character can use this '*', You can from dp[i - 1][j] Transfer , Show utilization '*'.
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(),n = p.size();
s = " " + s;
p = " " + p;
//dp[i][j] Express s Before i Characters and p Before j Whether the characters match
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];
}
};边栏推荐
- Confirm() method of window
- NATs cluster deployment
- 2022-2028 global ICT test probe industry research and trend analysis report
- Examples of topological sequences
- Yboj mesh sequence [Lagrange interpolation]
- 2022-2028 global capsule shell industry research and trend analysis report
- Redis - sentinel mode
- 2022-2028 global carbon fiber room scraper system industry research and trend analysis report
- The programmer's girlfriend gave me a fatigue driving test
- 2022-2028 global encrypted external hard disk industry research and trend analysis report
猜你喜欢

初识 Flutter 的绘图组件 — CustomPaint

Basic knowledge of Embedded Network - introduction of mqtt

CentOS install MySQL

MySQL variables, stored procedures and functions

2022-2028 global electric yacht industry research and trend analysis report

写给 5000 粉丝的一封信!

Pytorch auto derivation

Gateway service gateway

2022-2028 global capsule shell industry research and trend analysis report

2022-2028 global ICT test probe industry research and trend analysis report
随机推荐
P4学习——Basic Tunneling
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
Quick start of wechat applet -- project introduction
Redis - sentinel mode
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
[DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
The programmer's girlfriend gave me a fatigue driving test
2022-2028 global single travel industry research and trend analysis report
C language file operation for conquering C language
Self examination before school starts
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Yboj mesh sequence [Lagrange interpolation]
Can SQL execution be written in tidb dashboard
Never use redis expired monitoring to implement scheduled tasks!
[PHP] self developed framework qphp, used by qphp framework
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
【日常记录】——对BigDecimal除法运算时遇到的Bug
New trend of embedded software development: Devops