hdu 6170 正则表达式 dp

传送门

Two strings

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


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6170  6169  6168  6167  6166 
 

题意

给出原串与匹配串,问能否匹配原串中所有的字符。

 

思路

如果这是一个标准的正则匹配,是不是可以直接用语言特性了呢?

我们设原串为 a ,匹配串为 b , dp[i][j] 代表 b[1..i] 与 a[1..j] 是否匹配成功。

显然 dp[0][0] = true ,

对于其他情况:

  • 如果 b[i] == '.' ,则此时 a[j] 可以是任意字符, dp[i][j] 由 dp[i-1][j-1] 转移而来。

  • 如果 a[j] == b[i] ,同样 dp[i][j] 由 dp[i-1][j-1] 转移而来。

  • 如果 b[i] == '*' ,假设该 * 最终可以匹配 0 位,则 dp[i][j] 状态从 dp[i-2][j] 转移而来,假设最终匹配 1 位,则从 dp[i-1][j] 转移而来;

    假如 a[1..j-1] 与 b[1..i-1] 已成功匹配,并且 a[j-1] == a[j] ,显然当前的 * 可以继续匹配这一个字符,因此 dp[i][j] = true ;

    假如 a[1..j-1] 与 b[1..i] 已成功匹配(当前 * 已成功匹配若干位),且 a[j-1] == a[j] ,则可以继续匹配这一个字符,因此 dp[i][j] = true 。

  • 特别的,如果 b[2] == '*' ,则 dp[ALL][0] = true 。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=1e6+100;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int cas=1;
char a[maxn*3],b[maxn*3];
bool dp[maxn*3][maxn*3];
void solve()
{
    me(dp,false);
    a[0]=1,b[0]=1;
    gets(a+1);gets(b+1);
    int len1=strlen(a+1),len2=strlen(b+1);
    dp[0][0]=true;
    for(int i=1;i<=len2;i++)
    {
        if(i==2&&b[i]=='*')
            dp[i][0]=true;
        for(int j=1;j<=len1;j++)
        {
            if(b[i]=='.'||b[i]==a[j])
                dp[i][j]=dp[i-1][j-1];
            else if(b[i]=='*')
            {
                dp[i][j]=dp[i-2][j]||dp[i-1][j];
                if((dp[i-1][j-1]||dp[i][j-1])&&a[j-1]==a[j])
                    dp[i][j]=true;
            }
        }
    }
    if(dp[len2][len1])
        puts("yes");
    else puts("no");
}
int main()
{
    //freopen( "1010.in" , "r" , stdin );
    int t;
    scan_d(t);
    W(t--)
    {
        solve();
    }
}

另:regex大佬

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<regex>
using namespace std;
const int MAXN=2600;
string s,p;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>s>>p;
        string s1=".*";
        string s2="(a*|b*|c*|d*|e*|f*|g*|h*|i*|j*|k*|l*|m*|n*|o*|p*|q*|r*|s*|t*|u*|v*|w*|x*|y*|z*"
                  "|A*|B*|C*|D*|E*|F*|G*|H*|I*|J*|K*|L*|M*|N*|O*|P*|Q*|R*|S*|T*|U*|V*|W*|X*|Y*|Z*)";
        auto pos=p.find(s1);
        while(pos!=string::npos)
        {
            p.replace(pos,2,s2);
            pos=p.find(s1,pos+157);
        }
        regex pat(p);
        if(regex_match(s,pat)) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值