hdu 6170 Two strings dp

本文介绍了一个关于两个字符串的匹配问题,给出了具体的输入输出样例,并提供了详细的代码实现。该问题涉及正则表达式的匹配规则,包括特殊符号 . 和 * 的使用。

Two strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



Problem Description
Giving two strings and you should judge if they are matched.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
 

 

Input
The first line contains an integer T implying the number of test cases. (T≤15)
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
 

 

Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
 

 

Sample Input
3 aa a* abb a.* abb aab
 

 

Sample Output
yes yes no
 

 

Source

类似leetcode第10题;

https://leetcode.com/problems/regular-expression-matching/description/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
#define PI acos(-1.0)
const int N=3e3+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1e18+7;

char s[N],p[N];
bool dp[2510][2510];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,false,sizeof(dp));
        scanf("%s%s",s+1,p+1);
        dp[0][0]=1;
        int n=strlen(s+1);
        int m=strlen(p+1);
        for(int i=2;i<=m;i++)
        {
            dp[0][i]=p[i-1]&&dp[0][i-2];
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(p[j]=='*')
                {
                    dp[i][j] = dp[i][j-2]|| dp[i][j-1] ||( s[i] == p[j-1] || ( p[j-1] == '.'&& s[i]==s[i-1] ) )  && dp[i-1][j];
                }
                else
                dp[i][j] = (s[i] == p[j] || p[j] == '.') && dp[i-1][j-1];
            }
        }
        if(dp[n][m])printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/7412526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值