fork(1) download
  1. /**
  2. KMP
  3.  
  4. Date: January 18, 2017
  5. Runtime: 1.080/3.000s
  6.  */
  7.  
  8. #include <bits/stdc++.h>
  9.  
  10. #define READ(f) freopen(f, "r", stdin)
  11. #define WRITE(f) freopen(f, "w", stdout)
  12. #define FE(x, v) for (typeof v.begin() x = v.begin(); x != v.end(); ++x)
  13. #define FOR(i, a, n) for(int i = (a); i < (int)(n); ++i)
  14. #define REP(i, n) FOR(i, 0, n)
  15. #define el '\n'
  16. #define pb push_back
  17.  
  18. using namespace std;
  19.  
  20. typedef long long ll;
  21. typedef pair<int, int> ii;
  22.  
  23. const int M = 1005;
  24. string t, p;
  25. int tc, n, m, q, b[M];
  26.  
  27. inline void pp() {
  28. m = (int)p.size();
  29. int i = 0, j = -1; b[0] = -1;
  30. while (i < m) {
  31. while (j >= 0 && p[i] != p[j]) j = b[j];
  32. ++i; ++j; b[i] = j;
  33. }
  34. }
  35.  
  36. inline bool kmp() {
  37. n = (int)t.size();
  38. int i = 0, j = 0;
  39. while (i < n) {
  40. while (j >= 0 && t[i] != p[j]) j = b[j];
  41. ++i; ++j;
  42. if (j == m) return true;
  43. }
  44. return false;
  45. }
  46.  
  47. int main() {
  48. ios_base::sync_with_stdio(false); cin.tie(NULL);
  49. cin >> tc;
  50. REP(ttc, tc) {
  51. cin >> t >> q;
  52. REP(qq, q) {
  53. cin >> p; pp();
  54. cout << (kmp() ? "y\n" : "n\n");
  55. }
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0s 3468KB
stdin
2
abcdefghABCDEFGH
2
abc
abAB
xyz
1
xyz
stdout
y
n
y