题目:
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
The programming language Better And Portable Code (BAPC) is a language for working with lists of integers. The language has two built-in functions: ‘R’ (reverse) and ‘D’ (drop).
The function ‘R’ reverses its input list, and ’D’ drops the first
element of its input and returns the rest, or gives an error in case its
input is an empty list. To get more advanced behavior, functions can be
composed: “AB” is the function that first applies ‘A’ to its input and
then ‘B’ to the resulting list. For example, “RDD” is a function that
reverses a list and then drops the first two elements.
Unfortunately, our BAPC interpreter has bit rotted, so we ask you to
write a new one. Given a BAPC program and its input, return its output
or “error” in case ‘D’ is applied to an empty list. Lists are
represented as the character ‘[’ followed by a comma-separated list of
integers followed by the character ‘]’. Notice that the input and output
lists can be quite long.
Input
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with a string p (1 <= length(p) <= 100 000): a BAPC program, consisting of the characters ‘R’ and ‘D’.
- one line with an integer n (0 <= n <= 100 000): the number of elements in the input.
- one line with a list of n integers in the form [x1, ..., xn] (1 <= xi <= 100): the input list.
Output
Per test case:
- one line with the resulting integer list or “error” in case of an error.
Sample Input
4 RDD 4 [1,2,3,4] DD 1 [42] RRD 6 [1,1,2,3,5,8] D 0 []
Sample Output
[2,1] error [1,2,3,5,8] error
思路:
题目只有R和D两种操作,即删除掉第一个元素和将数组反序。一开始想要用容器模拟删除和反序两种操作,
但是发现如果进行多次反序肯定会消耗大量的时间,因为反序的时候要对几乎所有的元素进行移动。
为了快速的将数组反序,可以考虑用双端的链表来作为数据结构,然后每次只需要交换头尾指针即可。
这里用了一个bool类型的reverse来记录数组是否是反序状态,每次反序只需reverse=!reverse即可,同时用
head,rear两个下边记录当前数组的首位下标,对元素的删除转化为下标的移动。一开始没有注意每一位数的范围
是1到100,全部都当做1位数处理结果WA,重新审题后加入函数get_an_integer(int index,int count)来读取输入中的
每个整数。
最后输出时判断数组是否是反序状态再进行相应输出。
1 #include<iostream> 2 using namespace std; 3 4 const int Max_length=100000; 5 6 7 string RD,input; 8 int intergers[Max_length]; 9 10 void get_an_interger(int &index,int &count); 11 12 int main(){ 13 int testcases; 14 cin>>testcases; 15 while(testcases--){ 16 int num_of_data; 17 cin>>RD>>num_of_data>>input; 18 int head=0,rear=num_of_data-1; 19 int len_of_RD=RD.length(),len_of_input=input.length(); 20 bool reverse=false,error_exist=false; 21 int count=0; 22 for(int i=0;i<len_of_input;i++){ 23 //scan the input to refine the integers and store them in array 24 if(input[i]>='1'&&input[i]<='9') 25 get_an_interger(i,count); 26 } 27 if(count!=num_of_data) 28 cout<<"Wrong in get data"<<endl; 29 for(int i=0;i<len_of_RD;i++){ 30 if(RD[i]=='R'){ 31 reverse=!reverse; 32 }else{ 33 if(num_of_data==0){ 34 cout<<"error"<<endl; 35 error_exist=true; 36 break; 37 }else{ 38 if(reverse) 39 rear--; 40 else 41 head++; 42 num_of_data--; 43 } 44 } 45 } 46 if(!error_exist){ 47 cout<<'['; 48 if(num_of_data==0) 49 cout<<']'<<endl; 50 if(reverse){ 51 for(int i=0;i<num_of_data;i++){ 52 if(i==num_of_data-1) 53 cout<<intergers[rear-i]<<']'<<endl; 54 else 55 cout<<intergers[rear-i]<<','; 56 } 57 }else{ 58 for(int i=0;i<num_of_data;i++){ 59 if(i==num_of_data-1) 60 cout<<intergers[head+i]<<']'<<endl; 61 else 62 cout<<intergers[head+i]<<','; 63 } 64 } 65 } 66 } 67 return 0; 68 } 69 void get_an_interger(int &index,int &count){ 70 int interger=input[index]-'0'; 71 index++; 72 while(input[index]>='0'&&input[index]<='9'){ 73 interger=interger*10+input[index]-'0'; 74 index++; 75 } 76 intergers[count]=interger; 77 count++; 78 }