1. 程式人生 > >hdu 6170 正則表示式 dp

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;
}