HDU 6170 Two strings dp || 正则

7 篇文章 0 订阅

HDU 6170 Two strings dp || 正则

题目链接:two strings
Two strings

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 435

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
2017 Multi-University Training Contest - Team 9

题意:

2个串s和t,s包含大小写字母,t包含大小写和. 和* 。其中.代表通配,*代表可以出现任意次前一个字符,包括-1次把前面的抵消掉。问2个串是否匹配

题目分析:

dp[i][j]代表t的前i个字符和s的前j个是否匹配,对于s[j]==t[i]||t[i]=='.' 状态转移为dp[i][j]=dp[i-1][j-1];对于t[i]=='*' ,考虑次数取0和-1的情况,只要dp[i-1][j]或者dp[i-2][j] 匹配,dp[i][j] 就会匹配。同时,考虑已经匹配过的情况,即dp[i-1][j-1]||dp[i][j-1] 并且s[j]==s[j-1] 时,dp[i][j]=1
网上居然还有正则库通过的大牛,mark一下

//
//  main.cpp
//  HDU 6170 Two strings
//
//  Created by teddywang on 2017/8/22.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[2551],t[2551];
bool dp[2600][2600];
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        s[0]=t[0]=1;
        gets(s+1);
        gets(t+1);
        int ls=strlen(s)-1;
        int lt=strlen(t)-1;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=lt;i++)
        {
            if(i==2&&t[i]=='*') dp[i][0]=1;
            for(int j=1;j<=ls;j++)
            {
                if(t[i]=='.'||t[i]==s[j])
                    dp[i][j]=dp[i-1][j-1];
                else if(t[i]=='*')
                {
                    dp[i][j]=dp[i-1][j]|dp[i-2][j];
                    if((dp[i-1][j-1]||dp[i][j-1])&&s[j-1]==s[j])
                        dp[i][j]=1;
                }
            }
        }
        if(dp[lt][ls]==1) printf("yes\n");
        else printf("no\n");
    }
}

使用regex 正则库:

//
//  main.cpp
//  1010Two strings
//
//  Created by teddywang on 2017/8/22.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include <regex>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int T;
vector<string> tmp;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        string s,t;
        cin>>s>>t;
        int len=t.length();
        tmp.clear();
        for(int i=0;i<len;i++)
        {
            string tt=t.substr(i,1);
            if(t[i]=='*'&&t[i-1]=='.') continue;
            if(t[i]=='.'&&t[i+1]=='*')
                tt="(.)\\1*";
            tmp.push_back(tt);
        }
        string buf;
        for(int i=0;i<tmp.size();i++)
        {
            buf+=tmp[i];
        }
        regex re(buf);
        bool flag=regex_match(s,re);
        if(flag) printf("yes\n");
        else printf("no\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值