Ultraviolet A 12503 robot instructions (B)

Source: Internet
Author: User
Robot instructions

You have a robot standing on the originXAxis. The robot will be given some instructions. Your task is to predict its position after executing all the instructions.

 

  • Left: Move one unit left (DecreasePBy 1, wherePIs the position of the robot before moving)
  • Right: Move one unit right (increasePBy 1)
  • SameI: Perform the same action as inI-Th instruction. it is guaranteed thatIIs a positive integer not greater than the number of instructions before this.

 

Input

The first line contains the number of test casesT(T100). Each test case begins with an integerN(1N100), the number of instructions. Each of the followingNLines contains an instruction.

 

Output

For each test case, print the final position of the robot. Note that after processing each test case, the robot shocould be reset to the origin.

 

Sample Input

 

23LEFTRIGHTSAME AS 25LEFTSAME AS 1SAME AS 2SAME AS 1SAME AS 4

 

Sample output

 

1-5

Question

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 using namespace std; 6 int T, n, ans; 7 string s[105]; 8 int main(){ 9     scanf("%d", &T);10     while(T--){11         ans = 0;12         string t;int temp;13         scanf("%d", &n);14         for(int i = 1; i <= n; i++){15             cin>>s[i];16             if(s[i][0] == ‘S‘){17                 cin>>t;cin>>temp;18                 s[i] = s[temp];19             }20 21         }22         for(int i = 1; i <= n; i++){23             if(s[i][0] == ‘R‘) ans++;24             else ans--; 25         }26         printf("%d\n", ans); 27     } 28     29     return 0;30 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.