题目:

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 }

转载于:https://www.cnblogs.com/jolin123/p/3407222.html

Sicily 7974. Integer Lists 解题报告相关推荐

  1. Sicily 6768. Log Books 解题报告

    题目传送门:6768. Log Books 思路: 1. 这道题比较麻烦的是输入,时间的格式如12:04 ,这样后面再计算加减的时候会有问题,所以干脆转化成分钟,没一个时刻用该时刻是当天的第多少分钟表 ...

  2. Sicily 1156. Binary tree 解题报告

    题目地址:1156. Binary tree 思路: 简单的一道二叉树相关的题目,题目会给出一颗树现在的形态,然后用前序遍历这棵树的节点输出数据即可. 每个节点会输入该节点的identifier,有点 ...

  3. 解题报告:【kuangbin带你飞】专题九 连通图

    目录 A.POJ 1236 Network of Schools(有向图缩点) B.UVA 315 Network(找割点) C.UVA 796 Critical Links(桥) D.POJ 369 ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. LeetCode解题报告汇总

    LeetCode解题报告: [LeetCode]1.Two Sum - Yoona - 博客频道 - CSDN.NET [LeetCode]2.Add Two Numbers - Yoona - 博客 ...

  6. 攻防世界XCTF-MISC入门12题解题报告

    MISC属于CTF中的脑洞题,简直就是信息安全界的脑筋急转弯.你说它渣,它也有亮点,不好评说.这块最亮眼的入门题就属隐写术,出题人骚的狠.但是我感觉未来其中一个重点,就是大数据安全,从海量流量中捕获恶 ...

  7. 解题报告 之 POJ1087 A Plug for UNIX

    解题报告 之 POJ1087 A Plug for UNIX Description You are in charge of setting up the press room for the in ...

  8. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  9. [精品]CSAPP Bomb Lab 解题报告(六)

    接上篇[精品]CSAPP Bomb Lab 解题报告(五) gdb常用指令 设置Intel代码格式:set disassembly-flavor intel 查看反汇编代码:disas phase_1 ...

最新文章

  1. Centos 7 安装 rabbitmq 3.6.6
  2. SOA学习笔记(一)
  3. python中find函数忽略大小写_python字符串(大小写、判断、查找、分割、拼接、裁剪、替换、格式化)...
  4. 剑指offer--51.表示数值的字符串
  5. 远程连接本地mongodb 数据库
  6. prim算法_数据结构 7.4.1 最小生成树 Prim
  7. 【Matlab函数知识点合集】新手入门第十四天
  8. 读书笔记|从零开始做运营(入门篇)
  9. 中国传媒大学计算机课程表,中国传媒大学新闻学院课程表.doc
  10. 网易易盾js逆向分析
  11. install - graph-tool
  12. UserBehavior用户行为分析
  13. 村庄规划中的产业发展规划
  14. 百度竞价排名SEM介绍
  15. 计算网站流量,选择适合带宽或月流量
  16. sqlite的可视化管理工具SQLite Expert
  17. 博弈五子棋(人机对战)算法改进
  18. 解决了一个Web网页显示不全的BUG
  19. 中国水资源现状分析,水资源人均占有率不足「图」
  20. 知道RAD Studio Sydney(Delphi 10.4.2)这些,少走弯路

热门文章

  1. 微信支付服务器demo,集成微信支付(附带demo)
  2. php向页面中添加数据_PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码...
  3. nacos 配置动态刷新_nacos配置中心修改后刷新
  4. tomcat startup启动不起来 但也不报错_Resin 与 Tomcat 服务器对比
  5. java jdbc工具类抽取_JavaWeb入门(三):JDBC工具类的抽取
  6. 如何在 Linux 上用密码加密和解密文件
  7. Redis Key资源占用情况的可视化分析
  8. JavaScript初学者编程题(3)
  9. 一道关于整型提升/截断的经典练习题
  10. RNN情感分类问题实战