1. 程式人生 > >2017多校聯合第9場1010 Two String/hdu 6170(正則表示式/dp)

2017多校聯合第9場1010 Two String/hdu 6170(正則表示式/dp)

Two strings

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


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

題意

字串模式匹配,'.'匹配任何一個字元,'*'表示它的前一個字元可以任意出現(0次或多次),給出字串和模式串,詢問是否匹配

分析

和標準正則表示式不同的是".*"模式串在題意下不能匹配"abcde"這樣的字串

按題意".*"的意思是相同字元的0個或多個重複串

那麼我們把模式串中的".*"替換成"(.)\1*"即可

不會的東西好多,多校沒見過的解法也好多
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#include <regex>
#define lowbit(x) (x&-x)
#define e exp(1.0)
#define eps 1e-8
//ios::sync_with_stdio(false);
//    auto start = clock();
//    cout << (clock() - start) / (double)CLOCKS_PER_SEC;
typedef long long ll;
typedef long long LL;
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        string a,b;
        cin>>a>>b;
        b=regex_replace(b,regex("\\.\\*"),"(.)\\1*");
        regex_match(a,regex(b))?cout<<"yes"<<endl:cout<<"no"<<endl;
    }
    return 0;
}