Codeforces Round #504 A Single Wildcard Pattern Matching


思路:很简单的一道题,哎,都是泪,如果没有 * 号 ,直接判断是否相等即可,如果又,截取 * 两边字符串分别与 t 串比较,截取的两个字符串必须在 t  串的最左边和左右边才能匹配,噢对了 ,从长度上也可以判定一些数据,详细请看代码


#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long ll;
string a,b;
ll n,m;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    cin >> a >> b;
    if(n - m > 1)
    {
        cout << "NO" << endl;
        return 0;
    }
    ll x = a.find('*');
    if(x >= 0 && x < n )
    {
        if(n-m == 1)
        {
            string temp = a;
            temp.erase(x,1);
            if(temp == b)
            {
                cout << "YES" << endl;
                return 0;
            }
            else
            {
                cout << "NO" << endl;
                return 0;
            }
        }
        string a1,a2;
        ll A,B;
        a1 = a.substr(0,x);
        a2 = a.substr(x+1,n-x-1);
        A = b.find(a1);
        if(A != 0)
        {
            cout << "NO" << endl;
            return 0;
        }
        B = b.rfind(a2);
        if(B + a2.size() != m)
        {
            cout << "NO" << endl;
            return 0;
        }
        cout << "YES" << endl;
    }
    else
    {
        if(a != b)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/81784921