1197. Hotel
Constraints
Time Limit: 2 secs, Memory Limit: 32 MB
Description
Last year summer Max traveled to California for his vacation. He had a great time there: took many photos, visited famous universities, enjoyed beautiful beaches and tasted various delicious foods. It is such a good trip that Max plans to travel there one more time this year. Max is satisfied with the accommodation of the hotel he booked last year but he lost the card of that hotel and can not remember quite clearly what its name is. So Max searched in the web for the information of hotels in California and got piles of choice. Could you help Max pick out those that might be the right hotel?
Input
Input may consist of several test data sets. For each data set, it can be format as below:
For the first line, there is one string consisting of ‘*’, ‘?’ and ‘a’-‘z’ characters. This string represents the hotel name that Max can remember. The ‘*’ and ‘?’ is wildcard characters. ‘*’ matches zero or more lowercase character(s), and ‘?’ matches only one lowercase character.
In the next line there is one integer n (1 <= n <= 10000) representing the number of hotel Max found, and then n lines follow. Each line contains one string of lowercase character(s), the name of the hotel.
The length of every string doesn’t exceed 50.Output
For each test data set, just simply output one integer in a line telling the number of hotel in the list whose name matches the one Max remembered.
Sample Input
herbert2amazonherbert?ert*2amazonherbert*2amazonanythinghertber?2amazonherber
Sample Output
1020
Problem Source
ZSUACM Team Member
// Problem#: 1197
// Submission#: 3420067
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
using namespace std;
const int MAX_N = 55;
int N;
bool dp[MAX_N][MAX_N];
int main() {
std::ios::sync_with_stdio(false);
while (1) {
string w, s;
cin >> w;
if (cin.eof()) break;
cin >> N;
int ans = 0;
for (int i = 0; i < N; i++) {
cin >> s;
int wl = w.size(), sl = s.size();
for (int j = 0; j <= wl; j++)
for (int k = 0; k <= sl; k++) dp[j][k] = false;
dp[0][0] = true;
for (int j = 1; j <= wl; j++) {
for (int k = 0; k <= sl; k++) {
if ('a' <= w[j - 1] && w[j - 1] <= 'z' && k) dp[j][k] = dp[j - 1][k - 1] & (w[j - 1] == s[k - 1]);
if (w[j - 1] == '*') dp[j][k] = (k ? (dp[j][k - 1] | dp[j - 1][k]) : dp[j - 1][k]);
if (w[j - 1] == '?' && k) dp[j][k] = dp[j - 1][k - 1];
}
}
if (dp[wl][sl]) ans++;
}
cout << ans << endl;
}
return 0;
}