当前位置:网站首页>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)
边栏推荐
- [cjson] precautions for root node
- Microsoft announces that it will discontinue support for older versions of visual studio
- Drive safety coding & troubleshooting guide
- Detailed usage of vim editor
- Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ already exists; SQL statement:
- How to deploy dolphin scheduler 1.3.1 on cdh5
- Introduction to audio alsa architecture
- Pupanvr- an open source embedded NVR system (1)
- Token based authentication
- Summary of problems in rv1109/rv1126 product development
猜你喜欢

MySQL Linux Installation mysql-5.7.24

How to generate provincial data from county-level data in ArcGIS?

Harris corner detection principle-

【cjson】根节点注意事项

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

Ecosystem type distribution data, land use data, vegetation type distribution and nature reserve distribution data

Qinglong wool - Kaka

Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class

Pupanvr- an open source embedded NVR system (1)
随机推荐
Musk promotes the development of fascinating new products partners remind important questions
Some problems of silly girl solved
ShanMeng and Beijing Adoption Day start NFT digital collection public offering
How to count the total length of roads in the region and draw data histogram
Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)
20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song
Classes and objects, methods and encapsulation
Multi thread learning III. classification of threads
cellular automaton
Parallélisation de l'entraînement accéléré TF. Données. Générateur de données
Understanding of day16 array create query static and dynamic array array array performance in memory
22-2-28 there are many things to do at work today, ETH analysis
Stm32f4 ll library multi-channel ADC
LabVIEW about TDMS and Binary Storage Speed
Data processing and data set preparation
4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
Sentinel-2 data introduction and download
Qs100 at command mqtt access thingsboard
@Configurationproperties value cannot be injected