Codeforces 1023A Single Wildcard Pattern Matching(瞎搞)

题目链接:Single Wildcard Pattern Matching

题意

给定两个字符串 s s t,长度分别为 n n m s s 串中最多含有一个 * 通配符,通配符可以用任意字符串(也可能为空)替换,问能否将其中的通配符用某个字符串替换后,使 s t t 串相等。

输入

第一行为两个整数 n,m (1n,m2×105),第二行为一个长度为 n n 的字符串 s,第三行为一个长度为 m m 的字符串 t s s 串中只含有小写字母与最多一个 * 字符,t 串只包含小写字母。

输出

如果 s s 串可以转化为 t 串,则输出 YES Y E S ,否则输出 NO N O

样例

输入
6 10
code*s
codeforces
输出
YES
提示
* 改为 force 就可以令两个字符串相等。
输入
6 5
vk*cup
vkcup
输出
YES
提示
用空串代替 * 就可以令两个字符串相等。
输入
1 1
v
k
输出
NO
提示
两个字符串不相等,故答案为 NO N O
输入
9 6
gfgf*gfgf
gfgfgf
输出
NO
提示
不论用什么字符串替换 * 都无法使两个字符串相等。
题解

如果 s s 串不包含通配符,直接用 strcmp 比较,否则从前往后、从后往前与 t t 串进行比较,记录 t 串的分隔位,如果 s s 串的 * 之前与 t 的公共前缀与 * 之后与 t t 的公共后缀不相交,就输出 YES,否则输出 NO N O

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
using namespace std;

#define LL long long
const int maxn = 200000 + 100;
int n, m;
char s[maxn], t[maxn];

int main() {
    #ifdef Dmaxiya
    freopen("test.txt", "r", stdin);
//    freopen("10.out", "w", stdout);
    #endif // Dmaxiya

    while(scanf("%d%d", &n, &m) != EOF) {
        int l, r;
        int tl, tr;
        bool flag = false;
        scanf("%s%s", s, t);
        for(int i = 0; i < n; ++i) {
            if(s[i] == '*') {
                flag = true;
                l = i - 1;
                r = i + 1;
            }
        }
        if(!flag) {
            if(strcmp(s, t) == 0) {
                printf("YES\n");
            } else {
                printf("NO\n");
            }
            continue;
        }
        tl = -1;
        tr = m;
        for(int i = 0; i <= l; ++i) {
            if(s[i] != t[i]) {
                flag = false;
                break;
            }
            tl = i;
        }
        for(int i = 1; n - i >= r; ++i) {
            if(s[n - i] != t[m - i]) {
                flag = false;
                break;
            }
            tr = m - i;
        }
        if(flag) {
            if(tl < tr) {
                printf("YES\n");
            } else {
                printf("NO\n");
            }
        } else {
            printf("NO\n");
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值