Source Code
/// Frail the skin is dry and pale
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

using ll = long long;
using ii = pair<int, int>;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp make_pair

const int N = 5e4 + 5, LG = 18, MOD = 31607;
const double PI = acos(-1);
int get(char c) {
    if(c == 'D')return 0;
    if(c == 'U')return 0;
}
set<pair<ii,ii>> st;
void doWork() {

    string s;
    cin >> s;
    s += s;
    s += s;
    s += s;
    ii cur(0,0);
    for(int i = 0; i < s.size(); i++) {
        ii to = cur;
        if(s[i] == 'D')cur.F -= 1;
        if(s[i] == 'U')cur.F+= 1;
        if(s[i] == 'L')cur.S -= 1;
        if(s[i] == 'R')cur.S += 1;
        if(st.count({min(to,cur), max(to,cur)})){
            cout << i+1 << endl;
            return;
        }
        st.insert({min(to,cur), max(to,cur)});
    }

    cout << "-1\n";

}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE
    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }


    return 0;
}
Copy
Are We There Yet? DeadlyPillow
GNU G++17
1202 ms
262.1 MB
Memory Limit Exceeded