Is- dp求正则表达式

#include <iostream>

using namespace std;


int main()

{

    char a[101],b[101];

    int n;

    scanf("%s",a);

    scanf("%d",&n);

    while (n--) {

        scanf("%s",b);

        int i,j,k,l1=strlen(a),l2=strlen(b);

        bool d[101][101];

        for (i=l1; i>=0; i--)

            for (j=l2; j>=0; j--) {

                if (i==l1&&j==l2)

                    d[i][j]=1;

                else if (a[i]==b[j])

                    d[i][j]=d[i+1][j+1];

                else if (a[i]=='*') {

                    d[i][j]=0;

                    for (k=j; k<=l2; k++)

                        if (d[i+1][k]) {

                            d[i][j]=1;

                            break;

                        }

                }

                else d[i][j]=0;

            }

        if (d[0][0])

            printf("%s\n",b);

    }

    return 0;

}


/*

 Problem description

 You are implementing an operating system, and now need to write a program to list files in a directory: `ls'. You want the user to be able to list only files that match a given pattern that can include wildcards (*), for example *.c. A wildcard matches zero or more characters of any kind.

 

 

 Input

 The first line contains a string P, containing 1-100 characters 'a'-'z', '*' and '.'. This is the pattern. The second line contains an integer N, 1 <= N <= 100, which is the number of files in the directory. Then follows N lines containing the names of the files in the directory. Each line is a string containing 1{100 characters 'a'-'z' and '.'.

 

 Output

 The output shall consist of the lenames that match the pattern, P, each on its own line, in the same order that they were given as input.

 

 Sample Input

 *.*

 4

 main.c

 a.out

 readme

 yacc

 *a*a*a

 4

 aaa

 aaaaa

 aaaaax

 abababa

 Sample Output

 main.c

 a.out

 aaa

 aaaaa

 abababa

*/

正则表达式匹配是一个经典的算法问题,主要是判断一个字符串是否能够完全匹配给定的正则表式。 在LeetCode上,也有一道关于正则表达式匹配的题目,题目编号是10。这道题目要实现一个支持 '.' 和 '*' 的正则表达式匹配,其中 '.' 可以匹配任意单个字符,'*' 可以匹配零个或多个前面的元素。 解决这道题可以使用动态规划的思想,具体的思路如下: 1. 创建一个二维数组dpdp[i][j]表示s的前i个字符与p的前j个字符是否匹配。 2. 初始化dp为true,表示空字符串与空正则表达式是匹配的。 3. 初始化dp[i]为false,表示非空字符串与空正则表达式是不匹配的。 4. 初始化dp[j],如果p[j-1]是"*",则dp[j]的值取决于dp[j-2]的值,表示将p[j-2]与p[j-1]去掉后的正则表达式是否匹配空字符串。 5. 对于其它的dp[i][j],分成两种情况: - 如果p[j-1]是"."或者与s[i-1]相等,则dp[i][j]的值取决于dp[i-1][j-1]的值,表示将s[i-1]和p[j-1]去掉后的字符串是否匹配。 - 如果p[j-1]是"*",则dp[i][j]的值取决于以下两种情况: - dp[i][j-2]的值,表示将p[j-2]和p[j-1]去掉后的正则表达式是否匹配s的前i个字符。 - dp[i-1][j]的值,表示将s[i-1]与p[j-2]匹配后的字符串是否匹配p的前j个字符。 6. 最后返回dp[s.length()][p.length()]的值,表示整个字符串s与正则表达式p是否完全匹配。 以上是一种使用动态规划解决正则表达式匹配问题的思路,具体的实现可以参考LeetCode官方提供的递归思路的解法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [LeetCode算法 —— 正则表达式匹配(详解官方动态规划思想)](https://blog.csdn.net/weixin_42100963/article/details/106953141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值