当前位置:网站首页>Sword finger offer19 regular expression matching string dynamic programming
Sword finger offer19 regular expression matching string dynamic programming
2022-07-18 13:57:00 【Li Fan, hurry up】
Note:

f[i][j] Express s front i Characters and p front j Whether characters match
For convenience , Add a space before each string , Subscript becomes from 1 Start ,1 It means there is a match 1 Characters
Then it is to see whether it is according to the situation * Discussion by situation
No * It's easy to handle , Just write it directly , But as for * You have to enumerate
Look at it separately * Express 0 Characters 1 Characters …
Optimize this O(n) Complexity , Yes, it is f[i - 1][j] A comparison will show that he happens to be f[i][j] And s[i] & s[j-1] Operate
The code is as follows :
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size(), m = p.size();
s = ' ' + s, p = ' ' + p;
vector<vector<bool>> f(n + 1, vector<bool>(m + 1));
f[0][0] = true;
for(int i = 0; i <= n; i ++)
for(int j = 1; j <= m; j ++){
if(j + 1 <= m && p[j + 1] == '*') continue;
if(i && p[j] != '*')
f[i][j] = f[i - 1][j - 1] && (s[i] == p[j] || p[j] == '.');
else if(p[j] == '*'){
f[i][j] = f[i][j - 2] || i && f[i - 1][j] && (s[i] == p[j - 1] || p[j - 1] == '.');
}
}
return f[n][m];
}
};
边栏推荐
- Abbyy finereader 15 standard OCR character recognition and PDF editing software tool
- SQL必知必会挑战题答案
- 英语 | 阅读的逻辑 解题笔记
- Qtcpserver multithreading implementation
- Esp8266+blinker+web distribution network
- Modèles de boîtes, flux de documents, positionnement, mise en page et conception réactive
- 基于JSP+Servlet的高校人事管理系统
- iptables屏蔽ip某个端口访问
- Color supplement of MATLAB scientific research drawing (special part 6) - 336 traditional French colors
- Idea merges dev branch code into master and so on
猜你喜欢

贝加莱PLC-更改轴任务扫描时间和周期位置下发

marginalization
![ORA-600:[qertbGetPartitionNumber:qesma2],[],[],[]](/img/08/5c9a27c0b488f76e3815ce76047082.png)
ORA-600:[qertbGetPartitionNumber:qesma2],[],[],[]

Use case interpretation: openinstall multi scenario application analysis

阅读真题 | 真题阅读 做题记录 二

XML文件删除掉注释

文旅夜游项目助力夜间经济发展

Advanced principle of MySQL: MySQL execution process and order

Collection和Collections区别
![ORA-600:[qertbGetPartitionNumber:qesma2],[],[],[]](/img/08/5c9a27c0b488f76e3815ce76047082.png)
ORA-600:[qertbGetPartitionNumber:qesma2],[],[],[]
随机推荐
三极管的基础知识(下)②
职场必备 | 123页华为内部项目管理PPT
Baccalais PLC change axis task scanning time and cycle position issue
PMP每日一练 | 考试不迷路-7.15
【锁相环】基于MATLAB的全数字锁相环设计与仿真
Mysql 问题
剑指Offer19-正则表达式匹配-字符串-动态规划
The open and closed interval of the mean value theorem of higher numbers | integrals, the first mean value theorem of integrals and its generalization
2018年江苏省信息与未来程序设计小能手比赛试题--(新)鸡兔同笼标程
Introduction to T100 interface development steps
2018 Jiangsu Provincial Information and future programming expert competition test question -- (New) chicken and rabbit in the same cage standard schedule
[target tracking] image inter frame difference target detection based on background subtraction and MATLAB simulation
The digital transformation forum for small and medium-sized enterprises in Shandong Province was successfully held, and Jiuzhou cloud empowers small and medium-sized enterprises to upgrade their digit
文献学习(part99)--Fast unfolding of communities in large networks
Flutter ListView controller. Animateto is invalid
Hcip static comprehensive review experiment
Three lines (spring daily question 59)
T100自定义应用使用说明(azzi650)
【目标跟踪】基于背景消减的图像帧间差分法目标检测及matlab仿真
腾讯员工发帖找对象,表示偏爱程序员!评论火了......丨黑马头条
