fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long int
  3. #define fb(i,n) for(i=0;i<n;i++)
  4. #define sd(a) scanf("%d",&a)
  5. #define sld(a) scanf("%lld",&a)
  6. #define ss(a) scanf("%s",a)
  7. #define pd(a) printf("%d",a)
  8. #define pld(a) printf("%lld ",a)
  9. #define ps(a) printf("%s",a)
  10. #define pn() printf("\n")
  11. #define w(t) while(t--)
  12. #define mod 1000000007
  13. #define PI 3.141592653589793
  14.  
  15. using namespace std;
  16.  
  17. vector<ll>v;
  18.  
  19. bool binarysearch(ll key)
  20. {
  21. //cout<<"key "<<key<<endl;
  22. ll i=0,j=v.size()-1;
  23. while(i<=j)
  24. {
  25. ll mid=(i+j)/2;
  26. //cout<<v[mid]<<endl;
  27. if(v[mid]==key)
  28. {
  29. //cout<<v[mid]<<endl;
  30. return true;
  31. }
  32. else if(v[mid]<key)
  33. i=mid+1;
  34. else
  35. j=mid-1;
  36. }
  37. return false;
  38. }
  39.  
  40.  
  41. int main()
  42. {
  43. ios_base::sync_with_stdio(false);
  44. //ll t;sld(t);w(t){
  45. hash<string>h;
  46.  
  47. ll n,m,val,i;
  48. bool ok=false;
  49. cin>>n>>m;
  50. string s,s1;
  51. fb(i,n)
  52. {
  53. cin>>s;
  54. v.push_back(h(s));
  55. //out<<v[v.size()-1]<<" ";
  56. }
  57. sort(v.begin(),v.end());
  58. /*
  59.   fb(i,v.size())
  60.   {
  61.   cout<<v[i]<<" ";
  62.   }
  63.   pn();
  64.   */
  65. //binarysearch();
  66. char c;
  67. w(m)
  68. {
  69. ok=false;
  70. cin>>s;
  71. for(ll it=0;it<s.length();it++)
  72. {
  73. c=s[it];
  74. if(c=='a')
  75. {
  76. s[it]='b';
  77. val=h(s);
  78. if(binarysearch(val))
  79. {
  80. cout<<"YES"<<endl;
  81. ok=true;
  82. break;
  83. }
  84. s[it]='c';
  85. val=h(s);
  86. if(binarysearch(val))
  87. {
  88. cout<<"YES"<<endl;
  89. ok=true;
  90. break;
  91. }
  92. }
  93. else if(c=='b')
  94. {
  95. s[it]='a';
  96. val=h(s);
  97. if(binarysearch(val))
  98. {
  99. cout<<"YES"<<endl;
  100. ok=true;
  101. break;
  102. }
  103. s[it]='c';
  104. val=h(s);
  105. if(binarysearch(val))
  106. {
  107. cout<<"YES"<<endl;
  108. ok=true;
  109. break;
  110. }
  111. }
  112. else if(c=='c')
  113. {
  114. s[it]='a';
  115. val=h(s);
  116. if(binarysearch(val))
  117. {
  118. cout<<"YES"<<endl;
  119. ok=true;
  120. break;
  121. }
  122. s[it]='b';
  123. val=h(s);
  124. if(binarysearch(val))
  125. {
  126. cout<<"YES"<<endl;
  127. ok=true;
  128. break;
  129. }
  130. }
  131. s[it]=c;
  132. }
  133. if(!ok)
  134. cout<<"NO"<<endl;
  135. }
  136. return 0;
  137. }
Success #stdin #stdout 0s 3460KB
stdin
5 4
ab
cacab
cbabc
acc
cacab
abc
aa
acbca
cb
stdout
YES
YES
NO
YES