/* Name : Florin
Chirica
School : Cygnus
Computer
Grade : 8
Junior 5 Division
Contest #2
Language : C++
*/
#include<stdio.h>
#include<string.h>
#define LMAX
1<<10
char sep[]="
,";
char
x[LMAX],words[5][LMAX],w[LMAX];
inline bool
check(int i)
{
//Returns true if words[i] can be written
as w.
int
N=strlen(w)-1,j,n=strlen(words[i])-1,ok;
if(w[0]=='*')
{
while(N>=1)
{
if(w[N]=='?' ||
w[N]==words[i][n])
ok=1;
else
ok=0;
if(!ok)
return 0;
N--; n--;
}
return 1;
}
if(w[N]=='*')
{
for(j=0;j<N;j++)
{
if(w[j]=='?' ||
w[j]==words[i][j])
ok=1;
else
ok=0;
if(!ok)
return 0;
}
return 1;
}
if(n!=N)
return 0;
for(j=0;j<=N;j++)
{
if(w[j]=='?' || w[j]==words[i][j])
ok=1;
else
ok=0;
if(!ok)
return 0;
}
return 1;
}
int main()
{
int n=0;
char *p;
freopen("search.in","r",stdin);
freopen("search.out","w",stdout);
gets(x);
p=strtok(x,sep);
while(p)
{
strcpy(words[++n],p);
p=strtok(NULL,sep);
}
int num;
for(register int i=1;i<=5;i++)
{
num=0;
gets(w);
for(register int j=1;j<=n;j++)
if(check(j))
{
num++;
if(num==1)
printf("%s",words[j]);
else
printf(",
%s",words[j]);
}
if(num==0)
printf("No Match");
printf("\n");
}
return 0;
}