Iamtui1010

WORDSEARCH

Dec 12th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<vector>
  4. #include<string>
  5. #include<algorithm>
  6. #include<map>
  7.  
  8. #define long long long
  9. #define nln '\n'
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     //freopen("wordgame.inp", "r", stdin);
  16.     long n, m, q;
  17.     cin >> n >> m >> q;
  18.     vector<vector<char>> mat(n+1);
  19.     for (long i = 1; i <= n; ++i)
  20.     {
  21.         mat[i].resize(m+1, ' ');
  22.         for (long j = 1; j <= m; ++j)
  23.             cin >> mat[i][j];
  24.     }
  25.     vector<string> wrd(q+1, "");
  26.     for (long i = 1; i <= q; ++i)
  27.         cin >> wrd[i];
  28.  
  29.     vector<bool> ans(q+1, 0);
  30.  
  31.     // ngang
  32.  
  33.     for (long i = 1; i <= n; ++i)
  34.     {
  35.         string str = "";
  36.         for (long j = 1; j <= m; ++j)
  37.             str += mat[i][j];
  38.  
  39.         string tem = str;
  40.         reverse(tem.begin(), tem.end());
  41.  
  42.         for (long j = 1; j <= q; ++j)
  43.             if (!ans[j] && (str.find(wrd[j]) <= (long)str.size() || tem.find(wrd[j]) <= (long)tem.size()))
  44.                 ans[j] = 1;
  45.     }
  46.  
  47.     // doc
  48.     for (long i = 1; i <= m; ++i)
  49.     {
  50.         string str = "";
  51.         for (long j = 1; j <= n; ++j)
  52.             str += mat[j][i];
  53.  
  54.         string tem = str;
  55.         reverse(tem.begin(), tem.end());
  56.  
  57.         for (long j = 1; j <= q; ++j)
  58.             if (!ans[j] && (str.find(wrd[j]) <= (long)str.size() || tem.find(wrd[j]) <= (long)tem.size()))
  59.                 ans[j] = 1;
  60.     }
  61.  
  62.     // cheo chinh va cheo phu
  63.     map<long, string> dcc;
  64.     map<long, string> dcp;
  65.     for (long i = 1; i <= n; ++i)
  66.         for (long j = 1; j <= m; ++j)
  67.         {
  68.             dcc[i+j] += mat[i][j];
  69.             dcp[i-j] += mat[i][j];
  70.         }
  71.  
  72.     for (long i = 1; i <= n+m; ++i)
  73.     {
  74.         string tem = dcc[i];
  75.         reverse(tem.begin(), tem.end());
  76.         for (long j = 1; j <= q; ++j)
  77.             if (!ans[j] && (dcc[i].find(wrd[j]) <= dcc[i].size() || tem.find(wrd[j]) <= tem.size()))
  78.                 ans[j] = 1;
  79.     }
  80.  
  81.     for (long i = 1-m; i <= n-1; ++i)
  82.     {
  83.         string tem = dcp[i];
  84.         reverse(tem.begin(), tem.end());
  85.         for (long j = 1; j <= q; ++j)
  86.             if (!ans[j] && (dcp[i].find(wrd[j]) <= dcp[i].size() || tem.find(wrd[j]) <= tem.size()))
  87.                 ans[j] = 1;
  88.     }
  89.  
  90.     for (long i = 1; i <= q; ++i)
  91.         if (ans[i])
  92.             cout << "YES" << nln;
  93.         else
  94.             cout << "NO" << nln;
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment