7108
巧妙使用STL库函数map
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n;
ll ans;
map<pair<int,int>,int>pos;//注意map函数的使用
int main(){
int t;
ll x,y;
scanf("%d",&t);
char ch;
while(t--){
ans=x=y=0;
while(!pos.empty())pos.clear();
pos[{0,0}]=1;
scanf("%d%c",&n,&ch);//吃掉换行
while(n--){
scanf("%c",&ch);
if(ch=='U')++y;//上
else if(ch=='D')--y;//下
else if(ch=='L')--x;//左
else ++x;//右
if(pos.count({x,y})==0)pos[{x,y}]=1;//没出现过,记下这个点
else ans+=pos[{x,y}]++;//出现过了,加上
}
printf("%lld\n",ans);
}
return 0;
}