fork(1) download
#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0

struct node_st{
	char c;
	struct node_st *next;
	struct node_st *prev;
	struct node_st *jump;
}typedef node_st;

struct head_st{
	node_st *first;
	node_st *last;
}typedef head_st;

void Add(char c, head_st *h);
void SetJump(head_st *h);
int Check(head_st *h, char *S);
void FreeH(head_st *h);

int main(void){
	int T;
	char *L, *S;
	head_st *head;

	head = (head_st*)malloc(sizeof(head_st));

	scanf("%d", &T);
	for(int j=0; j<T; j++){
		head->first=NULL, head->last=NULL;

		scanf("%ms", &L);
		scanf("%ms", &S);

		for(int i=0; L[i] != '\0'; i++){
			Add(L[i], head);
		}

		SetJump(head);

		if(Check(head,S) == true)
			printf("Yes\n");
		else
			printf("No\n");

		FreeH(head);
		free(S);
		free(L);
	}

	free(head);
    
	return 0;
}

void Add(char c, head_st *h){
	node_st *node, *aux;

	//remover ? e * desnecessarios
	if(h->last != NULL){
		if(c == '?' && h->last->c == '*')
			return;
		else if(c == '*'){
			aux = h->last;
			while(aux != NULL && (aux->c == '?' || aux->c == '*'))
				aux = aux->prev;
			h->last=aux;

			if(aux == NULL)
				h->first = NULL;
		}
	}

	node = (node_st*)malloc(sizeof(node_st));
	
	node->c = c;
	node->jump = NULL;
	node->next = NULL;
	node->prev = h->last;
	
	if(h->first != NULL)
	       	h->last->next = node;
	else
		h->first = node;

	h->last = node;
}

void SetJump(head_st *h){
	node_st *node, *jmp=NULL;
	char c='-';
	
	node = h->last;

	while(node != NULL){
		if(node->c == '*' || node->c == '?')
			node->jump = jmp;
		else
			jmp = node;

		node = node->prev;
	}
}

int Check(head_st *h, char *S){
	node_st *node, *nodeRet;
	int i=0, indRet;

	nodeRet = NULL;
	node = h->first;

	while(node != NULL && S[i] != '\0'){
		//printf("1 - L: %c | S: %c\n",node->c,S[i]);
		if(node->c == '?'){
			if(node->jump != NULL && S[i] == node->jump->c){
				nodeRet = node->next;
				indRet = ++i;
				node = node->jump->next;
			}
			else{
				node = node->next;
				i++;
			}

		}
		else if(node->c == '*'){
			nodeRet = node;
			if(node->next == NULL){
				return true;
			}
			else if(node->jump != NULL && S[i] == node->jump->c){
				node = node->jump->next;
			}
			else{
				node = node->next;
			}
			indRet = ++i;
		}
		else{
			if(node->c != S[i])
				if(nodeRet == NULL)
					return false;
				else{
					node = nodeRet;
					i = indRet;
					nodeRet = NULL;
				}
			else{
				node = node->next;
				i++;
			}
		}

		if(node == NULL && S[i] != '\0'){
			if(nodeRet == NULL)
				return false;
			else{
				node = nodeRet;
				i = indRet;
				nodeRet = NULL;
			}
		}
	}

	while(node != NULL && (node->c == '*' || node->c == '?'))
		node = node->next;

	if(node == NULL && S[i] == '\0') return true;
	else return false;
}

void FreeH(head_st *h){
	node_st *aux, *aux2;

	aux = h->first;
	while(aux!=NULL){
		aux2=aux->next;
		free(aux);
		aux=aux2;
	}

}
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Standard output is empty