Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define NMAX 2023
- #define ll long long
- #define MOD (ll)(1e9 + 7)
- #include <bits/stdc++.h>
- using namespace std;
- ifstream f("02A.in");
- ofstream g("02A.out");
- int N, M, sz;
- const vector<int> dx{-1, 1, 0, 0};
- const vector<int> dy{0, 0, -1, 1};
- ll ans = 0;
- int m[2][NMAX];
- bool viz[2][NMAX];
- string word;
- // Roll the ball BUNA 11/20 Back
- bool inGrid(int x, int y) {
- return (x >= 0 && x < N && y >= 0 && y < M);
- }
- void DFS(int x, int y, int p) {
- viz[x][y] = true;
- if (p == word.size())
- ans = (ans + 1) % MOD;
- for (int i = 0; i < 4; ++i) {
- int xx = x + dx[i];
- int yy = y + dy[i];
- if (!viz[xx][yy] && inGrid(xx, yy) && m[xx][yy] == word[p])
- DFS(xx, yy, p + 1);
- }
- viz[x][y] = false;
- }
- void reset() {
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < M; ++j)
- viz[i][j] = false;
- return;
- }
- int main() {
- string s1, s2;
- cin >> s1 >> s2;
- for (int j = 0; j < s1.size(); ++j)
- m[0][j] = s1[j];
- for (int j = 0; j < s2.size(); ++j)
- m[1][j] = s2[j];
- cin >> word;
- char pref = word[0];
- N = 2;
- M = s1.size();
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < M; ++j)
- if (m[i][j] == pref) {
- DFS(i, j, 1);
- reset();
- }
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement