Description
There is a robot at the origin of the number line. The robot will execute a series of instructions, and your task is to predict where it will be after all the instructions have been executed.
LEFT: Move one unit to the left
RIGHT: move one unit to the right
·SAME AS i: Performs the same action as item i. The input guarantees that i is a positive integer and does not exceed the number of previously executed instructions
Input
Enter the number of data sets T (T<=100) in the first row. The first line of each set of data is an integer n (1<=n<=100), that is, the number of instructions. One instruction per line below. The commands are numbered 1~n according to the input order.
Output
For each set of data, output the final position of the robot. After processing a set of data, the robot should reset to the origin of the number axis.
Sample Input
2 3 LEFT RIGHT SAME AS 2 5 LEFT SAME AS 1 SAME AS 2 SAME AS 1 SAME AS 4
Sample Output
1 -5
Hint
#include<stdio.h> #include<string> #include<string.h> #include<algorithm> #include<iostream> using namespace std; char s[100]; int cnt[100]; intmain() { int T, n; scanf("%d", &T); while (T--) { int num = 0; scanf("%d", &n); getchar(); for (int i = 1; i <= n; i++) { cin>>s; if (s[0] == 'L') { on one--; cnt[i] = -1; } else if (s[0] == 'R') { num++; cnt[i] = 1; } else { int x; cin >> s >> x; num = num + cnt[x]; cnt[i] = cnt[x]; //cout << s[len-1]-'0'<<" "<<cnt[i]<<endl; } } printf("%d\n", num); } return 0; } /********************************************************************** Problem: 1112 User: leo6033 Language: C++ Result: AC Time:8 ms Memory:2024 kb **********************************************************************/