cqyz oj | Hotels

Description

  Small Mu lived a few years ago when the hotel to travel to Germany, I feel it is very unique, this trip going to have to live there. But he has not remember the name of the hotel, so he uses the wildcard "*" and "?" And 26 lowercase letters to describe. Note that, where "*" represents 0 or any number of letters, "?" Represents a letter.

  Mu small inn in the Internet to find a series of names, he wondered how many hotel rooms can match his description.

Input

  Comprising a plurality of sets of data. The first line of each data a string representing the name of a small inn Mu impression that description; followed by a behavior an integer n, the number of hotel some names found on the Internet; and then the next n lines of a character string representing the name of the inn available online.

Output

  For each set of data, output an integer that represents the number of small Mu impression hotel name matches the name of the hotel online.

Sample Input 1

herbert
2
amazon
herbert
?ert*
2
amazon
herbert
*
2
amazon
anything
herbert?
2
amazon
herber

Sample Output 1

1
0
2
0

Hint

n <= 10000, each character only by the '*' and '?', and 26 lowercase letters, string length does not exceed 50.

a [] represent a description of the impression
B [] represents a search to hotels
D [i] [j] represents a j-th front of the front of the i-th and b can not match
See Code equation
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 55;

char a[maxn], b[maxn];
int n, m;
bool d[maxn][maxn];
bool solve(){
    memset(d, 0, sizeof(d));
    m = strlen(b+1);
    d[0][0] = true;
    for(int i=1;i<=n;i++){
        if(a[i] == '*') d[i][0] = d[i-1][0];
        for(int j=1;j<=m;j++){
            bool t = false;
            if(a[i] == b[j] || a[i] == '?') t = t || d[i-1][j-1];
            if(a[i] == '*') t = t || d[i-1][j] || d[i][j-1];
            d[i][j] = t;
        }
    }
    return d[n][m];
}

int main(){
    int q, ans;
    while(scanf("%s", a+1) == 1){
        n = strlen(a+1);
        scanf("%d", &q);
        ans = 0;
        while(q--){
            scanf("%s", b+1);
            if(solve()) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
 

Guess you like

Origin www.cnblogs.com/de-compass/p/11649553.html