动态规划|正则化字符串匹配-VS环境下

在这里插入图片描述
https://leetcode-cn.com/problems/regular-expression-matching/

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#pragma warning(disable:4996)
string str1, str2;
const int maxn = 200;
bool dp[maxn][maxn] = {false};
int strLen1;
int strLen2;
bool characterMatch(int i,int j)
{
	if (i < 0)
	{
		return false;
	}
	if (str2[j] == '.')
	{
		return true;
	}
	return str1[i] == str2[j];
}

void match()
{
	//初始化dp
	fill(dp[0], dp[0] + maxn * maxn, false);
	//初始条件
	dp[0][0] = true;
	//边界
	int strLen1 = str1.size();
	int strLen2 = str2.size();
	//str1是被匹配的字符串 str2是正则化字符串
	for (int i = 0; i <= strLen1; i++)
	{
		for (int j = 1; j <= strLen2; j++)
		{
			if (str2[j-1] == '*')
			{
				if (characterMatch(i - 1, j - 2))
				{
					dp[i][j] = dp[i - 1][j];
				}
				else
				{
					dp[i][j] = dp[i][j - 2];
				}
			}
			else
			{
				if (characterMatch(i - 1, j - 1))
				{
					dp[i][j] = dp[i - 1][j - 1];
				}
			}
		}
	}
}
int main()
{
	freopen("data.txt", "r", stdin);
	freopen("result.txt", "w", stdout);
	cin >> str1 >> str2;
	match();
	int strLen1 = str1.size();
	int strLen2 = str2.size();
	//cout << dp[strLen1][strLen2];
	if (dp[strLen1][strLen2] == true)
	{
		cout << "true";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值