Jueves 11 de febrero de 2021, hace buen tiempo [No lamentes el pasado, no desperdicies el presente, no temas el futuro]
Contenido de este artículo
1. Introducción
Espada se refiere a la Oferta 19. Coincidencia de expresiones regulares
2. Programación dinámica
Esta pregunta sigue siendo muy difícil. Solo miré la solución de este gran tipo directamente. Siento que mientras dedique un tiempo a pensarlo detenidamente, la idea aún puede ser clara. Aquí está el código:
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size();
int n = p.size();
vector<vector<bool>> dp(m+1,vector<bool>(n+1,false));
for(int i=0;i<=m;++i){
for(int j=0;j<=n;++j){
// 分成空正则和非空正则两种
if(j==0) dp[i][j] = i==0;
// 非空正则分为两种情况 * 和 非 *
// 非 * 的情况(当j=n时,j-1表示正则串p的最后一个字符)
else if(p[j-1]!='*'){
if(i>0 && (p[j-1]==s[i-1] || p[j-1]=='.')){
dp[i][j] = dp[i-1][j-1];
}
}
// * 的情况
else if(p[j-1]=='*'){
// 碰到 * 了,分为考虑和不考虑两种情况
// 不考虑 *
if(j>=2){
dp[i][j] = dp[i][j-2];
}
// 考虑 *(当i=m时,i-1表示字符串s的最后一个字符))
if(i>=1 && j>=2 && (s[i-1]==p[j-2] || p[j-2]=='.')){
dp[i][j] = dp[i][j] || dp[i-1][j];
}
}
}
}
return dp[m][n];
}
};
referencias
"Oferta de Sword Finger Segunda Edición"
https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/solution/zhu-xing-xiang-xi-jiang-jie-you-qian-ru-shen- by-je /