【Codeforces Round #425 (Div. 2) B】Petya and Exam

Link:http://codeforces.com/contest/832/problem/B

Description

*能代替一个字符串(由坏字母组成);
?能代替单个字符(由好字母组成);
问你每个串能不能被匹配

Solution

对于没有*的情况;
判断长度是否相同,不相同则不行;
否则看看问号所在的位置是不是?且,看看?对应的字母是不是好字母;
对于有*的情况;
先把左边和右边的字符和母串的对应位置对应起来;
左对齐比较右对齐比较
然后中间部分就是用坏字母组成的字符串了;
对于母串的长度小于等于匹配串长度-2的直接输出无解;
对于母串长度等于匹配串长度-1的,除非匹配串里有*,否则直接输出无解;

NumberOf WA

1

Reviw

对于特殊情况;
还是分类讨论的方法比较好;
不然可能有意想不到的错误;
就比如这题;
我漏掉了长度不能相同;
而在长度不同的情况下对没有*的母串进行了匹配;

Code

 #include <bits/stdc++.h>
#define LL long long
using namespace std;

string s,ts;
int good[300],len,fi=-1,Q;

bool get_ans(){
    if ((int) ts.size()<= (int) s.size()-2) return false;
    if ((int) ts.size()== (int) s.size()-1 && fi==-1) return false;
    if (fi==-1 && (int) ts.size()!=(int)s.size()) return false;

    int l = 0,r = ts.size()-1;

    for (int i = 0;i <= fi-1;i++){
        if (s[i]!=ts[l]) {
            if (s[i]!='?')
                return false;
            else
                if (!good[ts[l]])
                    return false;
        }
        l++;
    }

    for (int i = len-1;i >=fi+1;i--){
        if (s[i]!=ts[r]){
            if (s[i]!='?')
                return false;
            else
                if (!good[ts[r]])
                    return false;
        }
        r--;
    }

    for (int i = l;i <= r;i++)
        if (good[ts[i]]) return false;
    return true;
}

int main(){
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio();
    cin >> s;
    len = s.size();
    for (int i = 0;i <= len-1;i++)
        good[s[i]] = 1;
    cin >> s;
    len = s.size();
    for (int i = 0;i <= len-1;i++)
        if (s[i]=='*')
            fi = i;
    scanf("%d",&Q);
    for (int i = 1;i <= Q;i++){
        cin >> ts;
        int ok = false;
        if (get_ans()){
            cout <<"YES"<<endl;
            continue;
        }
        cout<<"NO"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626168.html