Advertisement
i_love_rao_khushboo

Find all occurrences of the given string in a character matrix

Jul 5th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define mp make_pair
  10. #define F first
  11. #define S second
  12. #define PI 3.1415926535897932384626
  13. #define sz(x) ((int)(x).size())
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<string> vs;
  23. typedef vector<pii> vpii;
  24. typedef vector<pll> vpll;
  25. typedef vector<vi> vvi;
  26. typedef vector<vll> vvll;
  27. typedef vector<vull> vvull;
  28. typedef vector<vb> vvb;
  29. typedef vector<vc> vvc;
  30. typedef vector<vs> vvs;
  31.  
  32. /************************************************** DEBUGGER *******************************************************************************************************/
  33.  
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  36. #else
  37. #define debug(x)
  38. #endif
  39.  
  40. void _print(ll t) { cerr << t; }
  41. void _print(int t) { cerr << t; }
  42. void _print(string t) { cerr << t; }
  43. void _print(char t) { cerr << t; }
  44. void _print(ld t) { cerr << t; }
  45. void _print(double t) { cerr << t; }
  46. void _print(ull t) { cerr << t; }
  47.  
  48. template <class T, class V> void _print(pair <T, V> p);
  49. template <class T> void _print(vector <T> v);
  50. template <class T> void _print(vector <vector<T>> v);
  51. template <class T> void _print(set <T> v);
  52. template <class T, class V> void _print(map <T, V> v);
  53. template <class T> void _print(multiset <T> v);
  54. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  55. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  57. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60.  
  61. /*******************************************************************************************************************************************************************/
  62.  
  63. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  64. int rng(int lim) {
  65.     uniform_int_distribution<int> uid(0,lim-1);
  66.     return uid(rang);
  67. }
  68.  
  69. const int INF = 0x3f3f3f3f;
  70. const int mod = 1e9+7;
  71.  
  72. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  73.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  74.                          
  75. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  76. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  77.  
  78. /******************************************************************************************************************************/
  79.  
  80. vi dx = {-1, 0, 1, 0};
  81. vi dy = {0, 1, 0, -1};
  82.  
  83. bool is_valid(int x, int y, int n, int m) {
  84.     return x >= 0 and x < n and y >= 0 and y < m;
  85. }
  86.  
  87. void dfs(vvc &v, string &s, int i, int j, int n, int m, int idx, vpii &path, vvb &vis, vector<vpii> &res) {
  88.     if(idx == sz(s)) {
  89.         res.pb(path);
  90.         return;
  91.     }
  92.        
  93.     if(v[i][j] == s[idx]) {
  94.         vis[i][j] = 1;
  95.         path.pb({i, j});
  96.        
  97.         for(int k = 0; k < 4; k++) {
  98.             int nx = i + dx[k], ny = j + dy[k];
  99.             if(is_valid(nx, ny, n, m) and !vis[nx][ny]) {
  100.                 dfs(v, s, nx, ny, n, m, idx + 1, path, vis, res);
  101.             }
  102.         }
  103.        
  104.         vis[i][j] = 0;
  105.         path.ppb();
  106.     }
  107.    
  108.     else return;
  109. }
  110.  
  111. vector<vpii> find_word(vvc &v, string &s) {
  112.     int n = sz(v);
  113.     if(n == 0) return vector<vpii>();
  114.     int m = sz(v[0]);
  115.    
  116.     vector<vpii> res;
  117.     vvb vis(n, vb(m, 0));
  118.    
  119.     for(int i = 0; i < n; i++) {
  120.         for(int j = 0; j < m; j++) {
  121.             vpii path;
  122.             dfs(v, s, i, j, n, m, 0, path, vis, res);
  123.         }
  124.     }
  125.    
  126.     return res;
  127. }
  128.  
  129. void solve()
  130. {
  131.     int n, m; cin >> n >> m;
  132.     vvc v(n, vc(m));
  133.    
  134.     for(int i = 0; i < n; i++) {
  135.         for(int j = 0; j < m; j++) cin >> v[i][j];
  136.     }
  137.        
  138.     // key to be searched
  139.     string s; cin >> s;
  140.    
  141.     vector<vpii> res = find_word(v, s);
  142.    
  143.     for(int i = 0; i < sz(res); i++) {
  144.         for(int j = 0; j < sz(s); j++) {
  145.             cout << "'" << s[j] << "'";
  146.             cout << "(" << res[i][j].F << ", " << res[i][j].S << ") ";
  147.         }
  148.        
  149.         cout << "\n";
  150.     }
  151. }
  152.  
  153. int main()
  154. {
  155.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  156.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  157.  
  158.     // #ifndef ONLINE_JUDGE
  159.     //     freopen("input.txt", "r", stdin);
  160.     //     freopen("output.txt", "w", stdout);
  161.     // #endif
  162.    
  163.     // #ifndef ONLINE_JUDGE
  164.     //      freopen("error.txt", "w", stderr);
  165.     // #endif
  166.    
  167.     int t = 1;
  168.     // int test = 1;
  169.     // cin >> t;
  170.     while(t--) {
  171.         // cout << "Case #" << test++ << ": ";
  172.         solve();
  173.     }
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement