Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node{
- bool end;
- struct node* word[3];
- } ;
- node* head;
- void init(){
- head = new node();
- head->end = false;
- }
- void add(string s){
- node* current = head;
- for(int i = 0; i < s.size(); i++){
- int letra = s[i]-'a';
- if(!current->word[letra]){
- current->word[letra] = new node();
- }
- current = current->word[letra];
- }
- current->end = true;
- }
- bool search(string s, int id){
- if(head->word[id] == NULL) return false;
- node* current = head->word[id];
- //cout << id << endl;
- int erros = 0;
- for(int i = 0; i < s.size()-1; i++){
- int letra = s[i]-'a';
- if(current->word[letra] == NULL){
- if(erros) return false;
- erros++;
- int flag = false;
- for(int j = 0; j < 3; j++){
- if(current->word[j] != NULL){
- if(current->word[j]->word[s[i+1]-'a']){
- current = current->word[j];
- flag = true;
- break;
- }
- }
- }
- if(!flag) return false;
- }else{
- if(current->word[letra]->word[s[i+1]-'a']) current = current->word[letra];
- else{
- int flag = false;
- for(int j = 0; j < 3; j++){
- if(current->word[j] != NULL){
- if(current->word[j]->word[s[i+1]-'a']){
- current = current->word[j];
- flag = true;
- erros++;
- break;
- }
- }
- }
- if(!flag) return false;
- }
- }
- }
- if(current->word[s.size()-1] != NULL) return true;
- else{
- if(erros) return false;
- return true;
- }
- }
- int main(){
- int a, b; scanf("%d %d", &a, &b);
- init();
- for(int i = 0; i < a; i++){
- char txt[300007]; scanf("%s", txt);
- add(txt);
- }
- for(int i = 0; i < b; i++){
- char txt[300007]; scanf("%s", txt);
- if(search(txt, 0) || search(txt,1) || search(txt, 2)) printf("YES\n");
- else printf("NO\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement