Codeforces 1296C(map)
map开二维
题意:t(1e3)组输入,n和长度为n(2e5)的字符串,两端删除任意长度使得剩下的字符串的效果为0.有答案则输出剩余的字符串的左右边界下标,否则输出0.
思路:所有n的和小于2e5,复杂度n。开一个二维数组来存,n范围限制,所以用map。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> //const int maxn = 1e5+5; #define ll long long #define inf 0x3f3f3f3f #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} using namespace std; const int maxn =2e5+5; map<int,int>m[maxn<<1]; char c[210000]; int main() { int q; scanf("%d",&q); while(q--) { int n,x,y,ansl=0,ansr=0,minn=inf; cin>>n; cin>>c+1; for(int i=2*n+1;i>=1;i--) m[i].clear(); x=n+1,y=n+1; m[x][y]=1; for(int i = 1;i <= n; ++i) { if(c[i] == 'L') x--; else if(c[i] =='R')x++; else if(c[i] == 'U') y++; else y--; if(m[x][y] != 0) { int temp = m[x][y]; if(i-temp+1 < minn) { minn = i-temp+1; ansl = temp; ansr = i; } } m[x][y] = i+1; } if(minn == inf){ cout<<-1<<endl; } else { cout<<ansl<<" "<<ansr<<endl; } } }