malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int N, S, L, A[100009];   int c[100009], dp[100009];   deque<pair<int, int>> s, t, u;   int main() {     scanf( %d%d%d , &N, &S, &L);     for (int i = 1; i <= N; i++) dp[i] = 1e6;     for (int i = 1, ptr = 1; i <= N; i++) {       scanf( %d , &A[i]);       while (s.size() && s.back().first >= A[i]) s.pop_back();       s.push_back({A[i], i});       while (t.size() && t.back().first <= A[i]) t.pop_back();       t.push_back({A[i], i});       while (ptr < i && t.front().first - s.front().first > S) {         ptr++;         while (s.front().second < ptr) s.pop_front();         while (t.front().second < ptr) t.pop_front();       }       c[i] = ptr - 1;     }     for (int i = 1; i <= N; i++) c[i] = min(c[i], i - L + 1);     for (int i = 1, ptr = 0; i <= N; i++) {       if (i >= L) {         while (u.size() && u.back().first >= dp[i - L]) u.pop_back();         u.push_back({dp[i - L], i - L});       }       for (; ptr < c[i]; ptr++) {         while (u.size() && u.front().second <= ptr) u.pop_front();       }       if (u.size()) dp[i] = u.front().first + 1;     }     printf( %d n , dp[N] >= 1e6 ? -1 : dp[N]);   }
#include <bits/stdc++.h>   using namespace std;   void dfs(long long v, vector<long long> adj[], vector<bool>& visited) {     cout << char(v +  a );     visited[v] = true;     for (long long u : adj[v]) {       if (!visited[u]) dfs(u, adj, visited);     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     vector<long long> adj[26];     set<char> st;     vector<string> store;     for (long long i = 0; i < n; i++) {       string s;       cin >> s;       store.push_back(s);       st.insert(s[0]);       for (long long j = 0; j < s.length() - 1; j++) {         long long a = s[j] -  a , b = s[j + 1] -  a ;         adj[a].push_back(b);       }     }     queue<long long> q;     vector<bool> visited(26, false);     for (auto it : st) {       bool boo = true;       for (long long i = 0; i < store.size(); i++) {         string s = store[i];         for (long long j = 1; j < s.length(); j++) {           if (s[j] == it) {             boo = false;             goto h;           }         }       }     h:;       if (boo) {         q.push(it -  a );         visited[it -  a ] = true;       }     }     while (!q.empty()) {       long long v = q.front();       q.pop();       dfs(v, adj, visited);     }   }
#include <bits/stdc++.h>   using namespace std;   vector<int> set1;   vector<int> set2;   int col[100004], vis[100004];   int flag = 1;   class Graph {     int V;     list<int> *adj;       public:     Graph(int V);     void addEdge(int v, int w);     void BFS(int s);   };   Graph::Graph(int V) {     this->V = V;     adj = new list<int>[V];   }   void Graph::addEdge(int v, int w) {     adj[v].push_back(w);     adj[w].push_back(v);   }   void Graph::BFS(int s) {     list<int> queue;     vis[s] = 1;     queue.push_back(s);     col[s] = 0;     list<int>::iterator i;     while (!queue.empty()) {       s = queue.front();       queue.pop_front();       if (col[s] == 0) {         set1.push_back(s);       } else {         set2.push_back(s);       }       for (i = adj[s].begin(); i != adj[s].end(); ++i) {         if (vis[*i] != 1) {           vis[*i] = 1;           queue.push_back(*i);           col[*i] = (col[s] + 1) % 2;         } else {           if ((col[s] + 1) % 2 != col[*i]) {             flag = 0;             return;           }         }       }     }   }   int main() {     int n, m;     cin >> n >> m;     Graph g(n);     memset(col, -1, sizeof(col));     memset(vis, -1, sizeof(vis));     int a, b;     while (m--) {       cin >> a >> b;       vis[a - 1] = 0;       vis[b - 1] = 0;       g.addEdge(a - 1, b - 1);     }     for (int i = 0; i < n; i++) {       if (vis[i] == 0) g.BFS(i);       if (flag == 0) break;     }     if (flag == 0) {       cout <<  -1 n ;     } else {       cout << set1.size() <<   n ;       for (vector<int>::iterator i = set1.begin(); i != set1.end(); ++i)         cout << (*i) + 1 <<    ;       cout <<   n ;       cout << set2.size() <<   n ;       for (vector<int>::iterator i = set2.begin(); i != set2.end(); ++i)         cout << (*i) + 1 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int NMax = 100005;   const int sqrNMax = 330;   int N;   int Nb[NMax][sqrNMax + 5];   int A[NMax], B[sqrNMax + 5][2 * sqrNMax + 5], Start[sqrNMax + 5], Block[NMax],       totalB, Size[sqrNMax + 5];   void Read() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) scanf( %d , &A[i]);   }   void buildB() {     int currentBlock = 1, cnt = 0;     Start[1] = 1;     for (int i = 1; i <= N; i++) {       ++cnt;       if (cnt == sqrNMax) {         ++currentBlock;         Start[currentBlock] = i;         cnt = 1;       }       ++Size[currentBlock];       B[currentBlock][cnt] = A[i];       Nb[A[i]][currentBlock]++;       Block[i] = currentBlock;     }     totalB = currentBlock;   }   int findBlock(int pos) {     int sum = 0;     for (int i = 1; i <= totalB; i++) {       if (sum + Size[i] >= pos) {         Start[i] = sum + 1;         return i;       }       sum += Size[i];     }   }   int deleteElem(int block, int pos) {     int p = pos - Start[block] + 1;     int res = B[block][p];     Nb[res][block]--;     for (int j = p; j < Size[block]; j++) swap(B[block][j], B[block][j + 1]);     --Size[block];     return res;   }   void addElem(int block, int pos, int val) {     int p = pos - Start[block] + 1;     Nb[val][block]++;     B[block][++Size[block]] = val;     for (int j = Size[block] - 1; j >= p; j--) swap(B[block][j], B[block][j + 1]);   }   void makeCircularPerm(int left, int right) {     int b = findBlock(right);     int val = deleteElem(b, right);     b = findBlock(left);     addElem(b, left, val);   }   void buildA() {     int cnt = 0;     for (int i = 1; i <= totalB; i++) {       for (int j = 1; j <= Size[i]; j++) A[++cnt] = B[i][j];     }   }   void initialize() {     for (int i = 1; i <= totalB; i++) {       for (int j = 1; j <= Size[i]; j++) Nb[B[i][j]][i] = 0;       Size[i] = 0;       Start[i] = 0;     }   }   inline int decode(int nb, int lastans) { return (nb + lastans - 1) % N + 1; }   int Query(int left, int right, int val) {     int ans = 0, b = findBlock(left), b2 = findBlock(right);     for (int i = b + 1; i < b2; i++) ans += Nb[val][i];     int l = left - Start[b] + 1, r = right - Start[b2] + 1;     if (b == b2) {       for (int j = l; j <= r; j++)         if (B[b][j] == val) ++ans;       return ans;     }     for (int j = l; j <= Size[b]; j++)       if (B[b][j] == val) ans++;     for (int j = 1; j <= r; j++)       if (B[b2][j] == val) ++ans;     return ans;   }   void Solve() {     int Q;     scanf( %d , &Q);     buildB();     int lastans = 0, cntQ = 0;     for (int i = 1; i <= Q; i++) {       int type, l, r, k;       scanf( %d%d%d , &type, &l, &r);       l = decode(l, lastans);       r = decode(r, lastans);       if (l > r) swap(l, r);       if (type == 1) {         ++cntQ;         makeCircularPerm(l, r);         if (cntQ == sqrNMax) {           cntQ = 0;           buildA();           initialize();           buildB();         }       } else {         scanf( %d , &k);         k = decode(k, lastans);         lastans = Query(l, r, k);         printf( %d n , lastans);       }     }   }   int main() {     Read();     Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005], l[100005], r[100005];   int main() {     int n, m, i, j, x, y, pos;     cin >> n >> m;     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     l[1] = r[1] = 0;     for (i = 2; i <= n; i++) {       l[i] = l[i - 1];       if (a[i - 1] > a[i]) l[i]++;       r[i] = r[i - 1];       if (a[i - 1] < a[i]) r[i]++;     }     for (i = 1; i <= m; i++) {       cin >> x >> y;       if (r[x] == r[y] || l[x] == l[y])         cout <<  Yes  << endl;       else {         pos = int(upper_bound(l + 1, l + n + 1, l[x]) - l);         pos--;         if (l[pos] == l[x] && r[pos] == r[y])           cout <<  Yes  << endl;         else           cout <<  No  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct ter {     int cx, cy, dir, pt, rem;     ter(){};     ter(int _cx, int _cy, int _dir, int _pt, int _rem)         : cx(_cx), cy(_cy), dir(_dir), pt(_pt), rem(_rem){};   };   const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};   ter on;   queue<ter> Q;   int n, t[32], cx, cy, dir, pt, rem, dir1, dir2, sol, maxx = -3;   bool bio[312][312][8][31][6], mp[312][312];   void radi() {     while (!Q.empty()) {       on = Q.front();       cx = on.cx;       cy = on.cy;       dir = on.dir;       pt = on.pt;       rem = on.rem;       Q.pop();       if (bio[cx][cy][dir][pt][rem]) continue;       bio[cx][cy][dir][pt][rem] = 1;       if (mp[cx][cy] == 0) ++sol;       mp[cx][cy] = 1;       if (rem > 0) {         Q.push(ter(cx + dx[dir], cy + dy[dir], dir, pt, rem - 1));       } else {         if (pt == n) continue;         dir1 = (dir + 1) % 8;         dir2 = (dir + 7) % 8;         Q.push(ter(cx + dx[dir1], cy + dy[dir1], dir1, pt + 1, t[pt + 1] - 1));         Q.push(ter(cx + dx[dir2], cy + dy[dir2], dir2, pt + 1, t[pt + 1] - 1));       }     }   }   int main() {     std::ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> t[i];     Q.push(ter(155, 155, 0, 1, t[1] - 1));     radi();     cout << sol << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     map<int, int> a;     for (int i = 0; i < n; i++) {       int temp;       cin >> temp;       a[temp]++;     }     map<int, int> b;     for (int i = 0; i < n - 1; i++) {       int temp;       cin >> temp;       b[temp]++;     }     map<int, int> c;     for (int i = 0; i < n - 2; i++) {       int temp;       cin >> temp;       c[temp]++;     }     for (map<int, int>::iterator it = a.begin(); it != a.end(); ++it) {       if (it->second != b[it->first]) {         cout << it->first << endl;         break;       }     }     for (map<int, int>::iterator it = b.begin(); it != b.end(); ++it) {       if (it->second != c[it->first]) {         cout << it->first << endl;         break;       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int people_in_queue[n];     for (int i = 0; i < n; i++) {       scanf( %d  , &people_in_queue[i]);     }     int num_of_product[n][100], sum[n];     for (int i = 0; i < n; i++) {       sum[i] = 0;       for (int j = 0; j < people_in_queue[i]; j++) {         scanf( %d , &num_of_product[i][j]);         sum[i] = sum[i] + num_of_product[i][j];       }     }     int time[n];     for (int i = 0; i < n; i++) {       time[i] = sum[i] * 5 + people_in_queue[i] * 15;     }     int min = time[0];     for (int i = 0; i < n; i++) {       if (min > time[i]) {         min = time[i];       }     }     printf( %d , min);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   template <typename T>   void printV(const T &t) {     std::copy(t.cbegin(), t.cend(),               std::ostream_iterator<typename T::value_type>(std::cout,    ));     cout <<   n ;   }   template <typename T>   void printVV(const T &t) {     for (int i = 0; i < t.size(); i++) {       printf( %d:  , i);       printV(t[i]);     }   }   template <typename T, typename S>   void printVP(vector<pair<T, S>> vp) {     if (vp.empty()) {       cout <<   n ;       return;     }     for (int i = 0; i < vp.size() - 1; i++) {       cout <<  (  << vp[i].first <<     << vp[i].second <<  )  ;     }     cout <<  (  << vp[vp.size() - 1].first <<     << vp[vp.size() - 1].second          <<  ) n ;   }   template <typename T>   void printVVP(vector<vector<pair<T, T>>> vvp) {     for (int i = 0; i < vvp.size(); i++) {       cout << i <<  :  ;       printVP(vvp[i]);     }   }   vector<string> split(const string &s, char delim) {     vector<string> result;     stringstream ss(s);     string item;     while (getline(ss, item, delim)) {       result.push_back(item);     }     return result;   }   bool is_number(const string &s) {     string::const_iterator it = s.begin();     while (it != s.end() && isdigit(*it)) ++it;     return !s.empty() && it == s.end();   }   void fast() {     ios::sync_with_stdio(false);     cin.tie(NULL);   }   bool isValid(int r, int c, int R, int C) {     return (r >= 0 && r < R && c >= 0 && c < C);   }   struct Can {     long long x, r, id;   };   bool compareByX(const Can &lhs, const Can &rhs) { return lhs.x < rhs.x; }   class Person {    public:     string name, speak;     unordered_set<string> understands;     friend ostream &operator<<(ostream &os, const Person &p) {       os << p.name <<  (  << p.speak <<  ): ;       for (auto s : p.understands) os <<     << s;       return os;     }   };   vector<pair<int, int>> deltas = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};   const int INF = 1e9 + 10;   const long long inf = 1e18 + 10;   const long double PI = 3.14159265359;   int main() {     int N;     cin >> N;     vector<int> A(N), B(N);     for (int i = 0; i < (N); ++i) {       cin >> A[i];       B[i] = A[i];     }     sort(B.begin(), B.end());     unordered_map<int, int> m;     for (int i = 0; i < (N); ++i) m[B[i]] = i;     vector<int> C(N);     for (int i = 0; i < (N); ++i) {       C[i] = B[(m[A[i]] + 1) % N];     }     printV(C);   }
#include <bits/stdc++.h>   using namespace std;   const int ms = 100010;   vector<int> g[ms];   int in[ms], out[ms];   int main() {     int n, m;     int a, b;     cin >> n >> m;     for (int i = 0; i < m; ++i) {       scanf( %d%d , &a, &b);       if (a > b) swap(a, b);       in[a]++, out[b]++;       g[a].push_back(b);     }     long long res = 0;     for (int i = 0; i < n; ++i) {       res += 1ll * in[i + 1] * out[i + 1];     }     printf( %lld n , res);     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       scanf( %d , &a);       res -= 1ll * out[a] * in[a];       for (int j : g[a]) {         res -= 1ll * out[j] * in[j];         g[j].push_back(a);         out[a]++;         in[j]++;         out[j]--;         res += 1ll * out[j] * in[j];       }       g[a].clear();       in[a] = 0;       printf( %lld n , res);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   priority_queue<int, vector<int>, greater<int> > p;   bool visited[100005];   vector<int> G[100005];   int n, m;   void prime() {     p.push(1);     while (!p.empty()) {       int v = p.top();       p.pop();       if (visited[v]) continue;       visited[v] = true;       printf( %d  , v);       for (int i = 0; i < G[v].size(); i++) {         if (!visited[G[v][i]]) p.push(G[v][i]);       }     }   }   int main() {     scanf( %d%d , &n, &m);     int x, y;     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       G[x].push_back(y);       G[y].push_back(x);     }     memset(visited, false, sizeof(visited));     prime();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2005, P = 998244353;   using ll = long long;   ll qpow(ll a, ll b) {     ll ret = 1;     while (b) {       if (b & 1) ret = ret * a % P;       a = a * a % P;       b >>= 1;     }     return ret;   }   int n, a, b;   ll pw[N * N], pw1[N * N];   ll dp[N][N], f[N], g[N], h[N];   int main() {     ios::sync_with_stdio(false);     cin >> n >> a >> b;     ll p = a * qpow(b, P - 2) % P;     if (n == 1) {       cout << 0 << endl;       return 0;     }     if (n == 2) {       cout << 1 << endl;       return 0;     }     pw[0] = pw1[0] = 1;     for (int i = 1; i <= n * n; i++) {       pw[i] = pw[i - 1] * p % P;       pw1[i] = pw1[i - 1] * (P - p + 1) % P;     }     dp[0][0] = 1;     for (int i = 1; i <= n; i++) {       dp[i][0] = 1;       for (int j = 1; j <= i; j++)         dp[i][j] = (dp[i - 1][j] * pw1[j] + dp[i - 1][j - 1] * pw[i - j]) % P;     }     for (int i = 1; i <= n; i++) {       f[i] = 1;       for (int j = 1; j < i; j++) f[i] = (f[i] + P - f[j] * dp[i][j] % P) % P;     }     for (int i = 3; i <= n; i++) {       ll tt = i * (i - 1) / 2;       for (int j = 1; j < i; j++) {         ll tmp = (g[j] + h[i - j]) * f[j] % P * dp[i][j] % P;         tt = (tt + tmp) % P;       }       g[i] = tt * qpow(P + 1 - f[i], P - 2) % P;       tt = f[i] * g[i] % P;       for (int j = 1; j < i; j++) {         ll tmp = (g[j] + h[i - j]) * f[j] % P * dp[i][j] % P;         tt = (tt + tmp) % P;       }       h[i] = tt;     }     cout << g[n] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, f = 0, ans, m, a1[20], flag = 0, a2[20], b1[20], b2[20], num[20] = {0},            c = 0;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a1[i] >> a2[i];     for (int i = 0; i < m; i++) cin >> b1[i] >> b2[i];     for (int i = 0; i < n; i++) {       int cura = 0, curb = 0;       for (int j = 0; j < m; j++) {         if ((a1[i] == b1[j] && a2[i] != b2[j]) ||             (a1[i] == b2[j] && a2[i] != b1[j]))           num[a1[i]]++, cura++;         if ((a2[i] == b1[j] && a1[i] != b2[j]) ||             (a2[i] == b2[j] && a1[i] != b1[j]))           num[a2[i]]++, curb++;       }       if (cura > 0 && curb > 0) flag = 1;     }     for (int i = 0; i < m; i++) {       int cura = 0, curb = 0;       for (int j = 0; j < n; j++) {         if ((b1[i] == a1[j] && b2[i] != a2[j]) ||             (b1[i] == a2[j] && b2[i] != a1[j]))           cura++;         if ((b2[i] == a1[j] && b1[i] != a2[j]) ||             (b2[i] == a2[j] && b1[i] != a1[j]))           curb++;       }       if (cura > 0 && curb > 0) flag = 1;     }     for (int i = 0; i < 15; i++)       if (num[i] > 0) c++, f = i;     if (flag)       cout << -1 << endl;     else if (c > 1)       cout << 0 << endl;     else       cout << f << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 132674;   const int MOD = 1e9 + 7;   int main() {     set<int> ar;     set<int> to_buy;     set<int> to_sell;     long long multiplyier = 1;     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int temp;       string l;       cin >> l;       scanf( %d , &temp);       if (l.size() == 3) {         if (to_buy.size() > 0 && *to_buy.rbegin() > temp) {           to_buy.insert(temp);         } else if (to_sell.size() > 0 && *to_sell.begin() < temp) {           to_sell.insert(temp);         } else           ar.insert(temp);       } else {         if (ar.find(temp) != ar.end()) {           for (auto u : ar) {             if (u != temp) {               if (u < temp)                 to_buy.insert(u);               else                 to_sell.insert(u);             }           }           ar.clear();           multiplyier = (multiplyier * 2) % MOD;         } else if (to_buy.find(temp) != to_buy.end()) {           if (*to_buy.rbegin() != temp) {             cout << 0;             return 0;           }           to_buy.erase(temp);           for (auto u : ar) to_sell.insert(u);           ar.clear();         } else if (to_sell.find(temp) != to_sell.end()) {           if (*to_sell.begin() != temp) {             cout << 0;             return 0;           }           to_sell.erase(temp);           for (auto u : ar) to_buy.insert(u);           ar.clear();         }       }     }     cout << (multiplyier * ((int)ar.size() + 1)) % MOD;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, n, k, y, x, i, j, h, ans, c, d[100005], f[10];   char s[105], s1[105];   int main() {     scanf( %d%d%s , &n, &k, s);     for (i = 1; i <= k; i++) {       int f[1000] = {0};       scanf( %d %d , &a, &b);       x = 0;       int DP = 1, CP = 0, lens1 = 0;       int p1 = 0;       int p2 = -1;       for (j = a - 1; j < b; j++) {         s1[lens1++] = s[j];       }       while (CP < lens1 && CP >= 0) {         if (s1[CP] >= 48 && s1[CP] <= 57) {           f[s1[CP] - 48]++;           if (s1[CP] >= 48 && s1[CP] <= 57) {             s1[CP]--;           }           p1 = 1;           p2 = CP;         } else if (s1[CP] ==  <  || s1[CP] ==  > ) {           if (p1 == 2) {             s1[p2] = 47;           }           if (s1[CP] ==  > ) {             DP = 1;           } else {             DP = -1;           }           p1 = 2;           p2 = CP;         }         if (DP == 1) {           CP++;         } else           CP--;       }       for (j = 0; j < 10; j++) {         printf( %d  , f[j]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2010, M = N * N;   int n, m, dist[N][N], deg[N][N], fx[N][N], fy[N][N];   char str[N][N];   pair<int, int> p[M];   int hh, tt;   bool st[N][N];   void topsort() {     hh = 0;     tt = -1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         dist[i][j] = 1;         fx[i][j] = i;         fy[i][j] = j;         if (!deg[i][j]) p[++tt] = {i, j};       }     pair<int, int> u;     int x, y, distance;     while (hh <= tt) {       u = p[hh++];       x = u.first, y = u.second, distance = dist[x][y];       if (str[x][y] ==  U )         x--;       else if (str[x][y] ==  D )         x++;       else if (str[x][y] ==  L )         y--;       else if (str[x][y] ==  R )         y++;       if (x <= 0 || x > n || y <= 0 || y > m) continue;       deg[x][y]--;       if (!deg[x][y]) p[++tt] = {x, y};       if (dist[x][y] < distance + 1) {         dist[x][y] = distance + 1;         fx[x][y] = fx[u.first][u.second];         fy[x][y] = fy[u.first][u.second];       }     }   }   void dfs(int x, int y) {     if (st[x][y]) return;     p[tt++] = {x, y};     st[x][y] = 1;     if (str[x][y] ==  U )       x--;     else if (str[x][y] ==  D )       x++;     else if (str[x][y] ==  L )       y--;     else if (str[x][y] ==  R )       y++;     dfs(x, y);   }   int main() {     int T, res, x, y;     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= m; ++j) deg[i][j] = st[i][j] = 0;       for (int i = 1; i <= n; ++i) {         scanf( %s , str[i] + 1);         for (int j = 1; j <= m; ++j) {           int x = i, y = j;           if (str[i][j] ==  U ) x--;           if (str[i][j] ==  D ) x++;           if (str[i][j] ==  L ) y--;           if (str[i][j] ==  R ) y++;           if (x <= 0 || x > n || y <= 0 || y > m) continue;           deg[x][y]++;         }       }       topsort();       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= m; ++j)           if (deg[i][j]) {             tt = 0;             dfs(i, j);             for (int k = 0; k < tt; ++k) {               int x = p[k].first, y = p[k].second;               deg[x][y] = 0;               dist[x][y] += tt - 1;             }           }       res = 0;       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= m; ++j)           if (res < dist[i][j]) {             res = dist[i][j];             x = fx[i][j];             y = fy[i][j];           }       cout << x <<     << y <<     << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = 3.1415926536;   int n, m, spaces;   bool a[100005], vis[505][505];   void walk(int x, int y, string s) {     int counter = 1;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  U ) {         if (x > 1) {           x--;           if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1;         }       } else if (s[i] ==  D ) {         if (x < n) {           x++;           if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1;         }       } else if (s[i] ==  R ) {         if (y < m) {           y++;           if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1;         }       } else {         if (y > 1) {           y--;           if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1;         }       }       counter++;     }     if (a[s.size()]) spaces++;   }   int main() {     int x, y;     string s;     cin >> n >> m >> x >> y >> s;     spaces = n * m - 1;     vis[x][y] = 1;     walk(x, y, s);     cout <<  1  ;     for (int i = 1; i < s.size(); i++) cout << a[i] <<    ;     cout << spaces <<   n ;     return 0;   }