https://codeforces.com/contest/1296/problem/C

题意:给一个只包含RLDU(向上下左右走)字符的字符串,问哪一段连续字符,最终位置保持不变,要求去除的字符最短;

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf=0x3f3f3f3f;
 4 const int maxn=2e5+10;
 5 map<pair<int,int>,int>vis;
 6 char a[maxn];
 7 int main()
 8 {
 9     int T;
10     scanf("%d",&T);
11     while(T--){
12         vis.clear();
13         int n;
14         scanf("%d",&n);
15         scanf("%s",a+2);
16         int lr=0;
17         int sx=0;
18         int ans=inf;
19         int ansl,ansr;
20         vis[{0,0}]=1;
21         for(int i=2;i<=n+1;i++){
22             if(a[i]=='D') sx--;
23             if(a[i]=='U') sx++;
24             if(a[i]=='R') lr++;
25             if(a[i]=='L') lr--;
26             if(vis[{sx,lr}]){
27                 if(ans>(i-vis[{sx,lr}])){
28                     ans=i-vis[{sx,lr}];
29                     ansl=vis[{sx,lr}]+1;
30                     ansr=i;
31                 }
32             }
33             vis[{sx,lr}]=i;
34         }
35         if(ans==inf) printf("-1\n");
36         else{
37             printf("%d %d\n",ansl-1,ansr-1);
38         }
39     }
40     return 0;
41 }