其实这道题我想的思路很复杂,而且不会处理后面的重复指令。看到这篇文章我才有思路,卡在重复指令的处理,这里使用数组,才能记忆前面的指令。
#include<bits/stdc++.h>
using namespace std;
const int N=105;
int a[N];
char s[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]=='L')
a[i]=-1;
else if(s[0]=='R')
a[i]=1;
else
{
int q;
scanf("%s %d",s,&q);
a[i]=a[q];
}
sum+=a[i];
}
printf("%d\n",sum);
}
return 0;
}