当前位置:网站首页>19. regular expression matching
19. regular expression matching
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 19. Regular Expression Matching
Ideas : Dynamic programming
State definition :dp[i] [j] Representative string s Before i Characters and p Before j Whether characters match
Transfer equation : We need to pay attention to , because dp[0] [0] Represents the status of empty characters , therefore dp[i] [j] The corresponding added character is s[i-1] and p[j-1]
- When p[j-1] = '*' when ,dp[i] [j] In any of the following cases: true When is true:
- dp[i] [j-2]: About to combine characters p[j-2]p[j-1] regard as 0 Time
- dp[i-1] [j] And s[i-1]==p[j-2]: Instant character p[j-2] Whether it matches when it occurs more than once
- dp[i-1] [j] And p[j-2]==’.’: Instant character ’.' There are more 1 Whether the times match
- When dp[j-1]!=’*' when ,dp[i] [j] In any of the following cases: true When is true:
- dp[i-1] [j-1] And s[i-1]==p[j-1]: Instant character s[i-1] and p[j-1] matching
- dp[i-1] [j-1] And p[j-1]==’.’: I'm going to change the character ’.' Treat as character s[i-1] Whether time matches
initialization : It needs to be initialized dp First line , To avoid index out of bounds during state transition .
- dp[0] [0]=true: representative 2 Whether the empty characters match
- dp[0] [j]=dp[0] [j-2] And p[j-1] = ‘*’: First line s Is an empty string , So when p Can be matched only by the even bits of .
- Return value dp[m] [n]
class Solution {
public:
bool isMatch(string s, string p) {
int m=s.size(),n=p.size();
vector<vector<bool>> dp(m+1,vector<bool>(n+1));
dp[0][0]=true;
for(int i=2;i<=n;i++){
dp[0][i]=dp[0][i-2]&&p[i-1]=='*';
}
for(int i=1;i<=m;++i){
for(int j=1;j<=n;++j){
dp[i][j]=p[j-1]=='*'?dp[i][j-2]||(dp[i-1][j]&&(s[i-1]==p[j-2]||p[j-2]=='.')):(dp[i-1][j-1]&&(s[i-1]==p[j-1]||p[j-1]=='.'));
}
}
return dp[m][n];
}
};
Time complexity O(mn)
Spatial complexity O(mn)
边栏推荐
- Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- Multi thread learning III. classification of threads
- 1009 word search
- Walking "daily question" and "DP"
- What is reverse repurchase of treasury bonds? Is the reverse repurchase of treasury bonds safe?
- Data processing and data set preparation
- Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data
- ESP8266 Arduino OLED
- Thingsboard create RCP widget
猜你喜欢

2022“高考记忆” 已打包完成,请查收!

Big manufacturers compete to join rust, performance and safety are the key, and the 2021 rust developer survey report is announced

Development of video preview for main interface of pupanvr-ui
![[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e](/img/2e/97310ec36aeb1fc1e9c82361141a36.jpg)
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e

Harris corner detection principle-

cellular automaton

Pupanvr- an open source embedded NVR system (1)

Esp32-who face detection

MySQL Linux Installation mysql-5.7.24

According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"
随机推荐
Detailed usage of vim editor
Musk promotes the development of fascinating new products partners remind important questions
[backtracking based on bit operation] queen n problem 2
Day18 creation and restoration of sparse array
JS controls the display and hiding of tags through class
Introduction to audio alsa architecture
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort
Servlet core technology
Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
【C语言】实现字符串截取功能
JS disable mobile sharing
SQL transaction
C asynchronous programming (async and await) and asynchronous method synchronous invocation
Chapter 1
How to deploy PostgreSQL as a docker container
Minigui3 runs on Hisilicon hi3520d/hi3531 platform
2022“高考记忆” 已打包完成,请查收!
Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
ESP8266 Arduino OLED