刷题10. Regular Expression Matching
一、题目说明
这个题目是10. Regular Expression Matching,乍一看不是很难。
但我实现提交后,总是报错。不得已查看了答案。
二、我的做法
我的实现,最大的问题在于对.*的处理有问题,始终无法成功。
#include<iostream>
using namespace std;
class Solution{
public:
bool isMatch(string s,string p){
bool result = true;
if(s.length()<=0 && p.length()<=0){
return true;
}
if(p==".*"){
return true;
}
int sCurr=0,pCurr=0;
int lenS = s.length();
int lenP = p.length();
//count the num of .*
int numOfWildCard = 0;
while(pCurr<lenP){
if(p[pCurr]=='.' && pCurr+1<lenP && p[pCurr+1]=='*'){
numOfWildCard++;
}
pCurr++;
}
//cout<<numOfWildCard<<":";
pCurr = 0;
while(sCurr<lenS && pCurr<lenP){
if((pCurr+1<lenP) && p[pCurr]=='.' && p[pCurr+1]=='*'){
if(pCurr+2<lenP){
pCurr = pCurr+2;
while(sCurr<lenS && s[sCurr]!=p[pCurr]){
sCurr++;
}
}
}
if((pCurr+1<lenP) && p[pCurr+1]=='*'){
while(sCurr<lenS && s[sCurr]==p[pCurr]){
sCurr++;
}
pCurr = pCurr+2;
}
if(sCurr<lenS && pCurr<lenP && p[pCurr+1]!='*'){
if(s[sCurr]==p[pCurr] || p[pCurr]=='.'){
sCurr++;
pCurr++;
}
}
}
if(sCurr==lenS && pCurr==lenP){
result = true;
}else{
result = false;
}
return result;
}
};
int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
return 0;
}
三、正确的做法
1.递归方法
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
bool isMatch(string s, string p) {//aa a
if(p.empty()) return s.empty();
if(s.empty()) return p.empty() || (p[1] == '*' ? isMatch(s, p.substr(2)) : false);
if(p[0] != '.' && s[0] != p[0]) return p[1] == '*' ? isMatch(s, p.substr(2)) : false;
if(p[1] == '*') return isMatch(s.substr(1), p) || isMatch(s, p.substr(2));
return isMatch(s.substr(1), p.substr(1));
}
};
int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
cout<<(false==s.isMatch("ab",".*c"))<<endl;
cout<<(true==s.isMatch("aaa","a*a"))<<endl;
return 0;
}
2.DP方法
dp是什么?动态规划啊,
#include<iostream>
#include<vector>
#include <mem.h>
using namespace std;
class Solution {
public:
bool isMatch(string s, string p) {
int ssize = s.size(),psize = p.size();
string pp="";
vector<bool> star;
for(int i=0;i<p.size();i++){
if(p[i]=='*'){
star.back()=true;
}else{
star.push_back(false);
pp+= p[i];
}
}
psize = pp.size();
bool dp[psize+1][ssize+1];
memset(dp,false,sizeof(dp));
dp[0][0] = true;
for(int i=1;i<=psize;i++){
if(star[i-1]==true){
dp[i][0] = true;
}else{
break;
}
}
for(int i=1;i<=psize;i++)
for(int j=1;j<=ssize;j++){
if(dp[i-1][j-1]== true){
if(pp[i-1]==s[j-1] || pp[i-1]=='.'){
dp[i][j] = true;
continue;
}
}
if(dp[i-1][j]== true){
if(star[i-1]==true){
dp[i][j] = true;
continue;
}
}
if(dp[i][j-1]== true){
if(star[i-1]==true && (pp[i-1]==s[j-1] || pp[i-1]=='.')){
dp[i][j] = true;
continue;
}
}
}
return dp[psize][ssize];
}
};
int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
cout<<(false==s.isMatch("ab",".*c"))<<endl;
cout<<(true==s.isMatch("aaa","a*a"))<<endl;
cout<<(false==s.isMatch("a",""))<<endl;
return 0;
}
四、总结
看来基础知识还需要恶补,加油!
刷题10. Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
随机推荐
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem
//只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...
- os.getcwd()和os.path.realpath(__file__)的区别
https://blog.csdn.net/xiaminli/article/details/74944580 python中split().os.path.split()函数用法
- 题解【AcWing274】移动服务
题面 非常好的优化 \(\text{DP}\) 状态表示的题目. 首先可以设 \(dp_{i,x,y,z}\) 表示已经做完了前 \(i\) 个请求,现在的 \(3\) 名服务员分别在 \(x\) . ...
- selenium获取短暂出现元素的xpath路径
1. pip install beautifulsoup4 :安装beautifulsoup4 2. from bs4 import BeautifulSoup 3. bs = BeautifulS ...
- abb画学号
MODULE Module2 VAR signaldi signaldi26; VAR signaldi signaldi37; VAR signaldi signaldi48; PROC main2 ...
- vue基础实例
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"> ...
- [C语言学习笔记四]变量与系统的交互
使用 const 创建常量和使用 volatie 优化变量 C语言中使用 const 定义常量. 例如: const INT a = 10; 此处如果添加a = 20;,编辑器则会报错,因为此处 a ...
- js/jquery加入的select value显示不正确问题
最近有需求,通过js添加select到表格中,虽然通过append加入的代码中select的value为2 但是实际显示出来的结果不对,百度之后没有找到答案,自己想了个办法 var selects = ...
- LTE引理——解决数论竞赛题的利器
LTE (Lifting The Exponent Lemma)引理是一个解指数型不定方程的强力工具.它在Olympiad folklore非常知名,虽然它的起源已经无从查找了.它和Hensel’s ...
- winform学习(5)MDI窗体
SDI窗体 single document interface 单文档界面,即单个简单的窗体 MDI窗体 multiple document interface 多文档界面(主窗体与子窗体的关系,避免 ...