Sicily 7974. Integer Lists

// Problem#: 7974
// Submission#: 3593659
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Solution to Integer Lists
// Author: Thomas Beuman

// Time complexity: O(p+n)
// Memory: O(n)

// @EXPECTED_RESULTS@: CORRECT

// Solution method: use array, with pointers to first and last element

#include <cstdio>
using namespace std;

int L[100000];

int main()
{ int cases, casenr, n, i, first, last;
  bool reversed;
  char p[100001];
  scanf("%d", &cases);
  for (casenr = 1; casenr <= cases; casenr++)
  { // Read input
    scanf("%s\n%d\n[", p, &n);
    for (i = 0; i < n; i++)
      scanf("%d%*c", &L[i]);
    scanf("%*c "); // Read redundant characters (']', newline)
    first = 0;
    last = n-1;
    reversed = false;
    // Process program
    for (i = 0; p[i] != '\0'; i++)
    { if (p[i] == 'R')
        reversed = !reversed;
      else if (reversed)
        last--;
      else
        first++;
    }
    // Output
    if (first > last+1)
      printf("error\n");
    else if (first == last+1) // Empty list
      printf("[]\n");
    else
    { if (reversed)
        for (i = last; i >= first; i--)
          printf("%c%d", i == last ? '[' : ',', L[i]);
      else
        for (i = first; i <= last; i++)
          printf("%c%d", i == first ? '[' : ',', L[i]);
      printf("]\n");
    }
  }
  return 0;
}                                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值