Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<vector>
- #include<string>
- #include<algorithm>
- #include<map>
- #define long long long
- #define nln '\n'
- using namespace std;
- int main()
- {
- //freopen("wordgame.inp", "r", stdin);
- long n, m, q;
- cin >> n >> m >> q;
- vector<vector<char>> mat(n+1);
- for (long i = 1; i <= n; ++i)
- {
- mat[i].resize(m+1, ' ');
- for (long j = 1; j <= m; ++j)
- cin >> mat[i][j];
- }
- vector<string> wrd(q+1, "");
- for (long i = 1; i <= q; ++i)
- cin >> wrd[i];
- vector<bool> ans(q+1, 0);
- // ngang
- for (long i = 1; i <= n; ++i)
- {
- string str = "";
- for (long j = 1; j <= m; ++j)
- str += mat[i][j];
- string tem = str;
- reverse(tem.begin(), tem.end());
- for (long j = 1; j <= q; ++j)
- if (!ans[j] && (str.find(wrd[j]) <= (long)str.size() || tem.find(wrd[j]) <= (long)tem.size()))
- ans[j] = 1;
- }
- // doc
- for (long i = 1; i <= m; ++i)
- {
- string str = "";
- for (long j = 1; j <= n; ++j)
- str += mat[j][i];
- string tem = str;
- reverse(tem.begin(), tem.end());
- for (long j = 1; j <= q; ++j)
- if (!ans[j] && (str.find(wrd[j]) <= (long)str.size() || tem.find(wrd[j]) <= (long)tem.size()))
- ans[j] = 1;
- }
- // cheo chinh va cheo phu
- map<long, string> dcc;
- map<long, string> dcp;
- for (long i = 1; i <= n; ++i)
- for (long j = 1; j <= m; ++j)
- {
- dcc[i+j] += mat[i][j];
- dcp[i-j] += mat[i][j];
- }
- for (long i = 1; i <= n+m; ++i)
- {
- string tem = dcc[i];
- reverse(tem.begin(), tem.end());
- for (long j = 1; j <= q; ++j)
- if (!ans[j] && (dcc[i].find(wrd[j]) <= dcc[i].size() || tem.find(wrd[j]) <= tem.size()))
- ans[j] = 1;
- }
- for (long i = 1-m; i <= n-1; ++i)
- {
- string tem = dcp[i];
- reverse(tem.begin(), tem.end());
- for (long j = 1; j <= q; ++j)
- if (!ans[j] && (dcp[i].find(wrd[j]) <= dcp[i].size() || tem.find(wrd[j]) <= tem.size()))
- ans[j] = 1;
- }
- for (long i = 1; i <= q; ++i)
- if (ans[i])
- cout << "YES" << nln;
- else
- cout << "NO" << nln;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment