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

Keywords: less iOS

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

[Description]

* Can replace a string (composed of bad letters);
Can replace a single character (composed of good letters);
Ask you if each string can be matched

[Solution]

For the case where there is no *
First judge whether the length is the same, but not the same.
Otherwise, look at the location of the question mark, and see if the corresponding letters are good letters.
For cases where * exists;
Firstly, the corresponding positions of the left and right characters and the parent string are corresponded.
Left alignment comparison and right alignment comparison
Then the middle part is a string of bad letters.
There is no solution for the direct output of the parent string whose length is less than or equal to the length of the matched string - 2.
If the length of the parent string equals to the length of the matched string - 1, there is no direct output solution unless there is * in the matched string.

[NumberOf WA]

1

[Reviw]

For special cases;
It's better to discuss them by classification.
Otherwise, there may be unexpected mistakes.
For example, this question;
I missed that the length can't be the same.
In the case of different lengths, the parent string without * is matched.

[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;
}

Posted by baennaeck on Sat, 09 Feb 2019 10:24:17 -0800