Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-12-25 23:37:48
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- void solve()
- {
- // subarray with sum 0, minimum size
- int n;
- cin >> n;
- string s;
- cin >> s;
- map<pair<int, int>, int> mp;
- mp[{0, 0}] = 0;
- int hor = 0, ver = 0, start = -1, end = -1, min_len = LLONG_MAX;
- for (int i = 0; i < n; i++)
- {
- if (s[i] == 'L')
- {
- hor--;
- }
- else if (s[i] == 'R')
- {
- hor++;
- }
- else if (s[i] == 'U')
- {
- ver++;
- }
- else if (s[i] == 'D')
- {
- ver--;
- }
- // if they have same value of hor, ver means in b/w we have a subarray with sum 0
- if (mp.count({hor, ver}))
- {
- int prev_index = mp[{hor, ver}];
- if (i - prev_index < min_len)
- {
- min_len = i - prev_index;
- start = prev_index + 1;
- end = i + 1;
- }
- }
- mp[{hor, ver}] = i + 1;
- }
- if (start == -1)
- {
- cout << -1 << endl;
- }
- else
- {
- cout << start << " " << end << endl;
- }
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment