2017 Multi-University Training Contest - Team 9 1010 Two strings

27 篇文章 0 订阅

Two strings

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


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
 
题目大意:问给出的正则表达式能否匹配文本

解题思路:c++regex   注意.*不能匹配ab

这个真不会呀啊啊啊啊啊啊 = =

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值