C. Compression and Expansion

文章描述了一种问题,即如何根据被替换为单个数字的嵌套列表恢复原始结构。给出了一个操作规则和输入输出样例,要求编写代码帮助William恢复嵌套列表。代码示例展示了如何使用栈来模拟操作并输出恢复后的列表。
摘要由CSDN通过智能技术生成

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands.

A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items a1.a2.a3.⋯.aka1.a2.a3.⋯.ak and can be one of two types:

  1. Add an item a1.a2.a3.⋯.ak.1a1.a2.a3.⋯.ak.1 (starting a list of a deeper level), or
  2. Add an item a1.a2.a3.⋯.(ak+1)a1.a2.a3.⋯.(ak+1) (continuing the current level).

Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section.

When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number.

William wants you to help him restore a fitting original nested list.

2.Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤101≤t≤10). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1031≤n≤103), which is the number of lines in the list.

Each of the next nn lines contains a single integer aiai (1≤ai≤n1≤ai≤n), which is what remains of William's nested list.

It is guaranteed that in each test case at least one fitting list exists.

It is guaranteed that the sum of values nn across all test cases does not exceed 103103.

3.Output

For each test case output nn lines which represent a valid nested list, which could become the data provided to you by William.

If there are multiple answers, print any.

4.Examples

4.1input

2
4
1
1
2
3
9
1
1
1
2
2
1
2
1
2

4.2output

1
1.1
1.2
1.3
1
1.1
1.1.1
1.1.2
1.2
1.2.1
2
2.1
2.2

5.Code

#include <iostream>
#include <stack>
#include <vector>
#include <utility>

using namespace std;

using ll = long long;
using pii = pair<int, int>;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        stack<int> stk;
        for (int i = 0; i < n; i++) {
            if (a[i] == 1) {
                stk.push(1);
            } else {
                while (!stk.empty() && stk.top() != a[i] - 1) {
                    stk.pop();
                }
                stk.push(a[i]);
            }
            int sz = stk.size();
            int j = 0;
            for (auto it = stk.begin(); it != stk.end(); ++it, ++j) {
                cout << *it;
                if (j < sz - 1) {
                    cout << ".";
                }
            }
            cout << endl;
        }
    }
    return 0;
}

6.Conclusion

1.从标准输入中读取一个整数 T,表示测试用例的数量。
2.随后执行 T 次测试用例,每个测试用例的处理如下:
        a. 从标准输入中读取一个整数 n,表示数组 a 的大小。
        b. 从标准输入中读取 n 个整数,将它们存储在名为 a 的整数向量中。
        c. 使用一个名为 stk 的整数栈来模拟特定的操作,栈中存储的是数组 a 的元素。
        d. 遍历数组 a 的元素,对于每个元素执行以下操作:

        如果当前元素是1,将1推入栈 stk。
        否则,检查栈 stk 是否为空,如果不为空并且栈顶元素不等于当前元素减1,就一直弹出栈元素,直到栈为空或者栈顶元素等于当前元素减1,然后将当前元素推入栈 stk。
        e. 计算栈 stk 的大小,然后遍历栈中的元素,将它们输出到标准输出,每个元素之间用点号 "." 分隔。
        f. 输出一个换行符,以表示当前测试用例的处理结束。
3.继续下一个测试用例,直到所有测试用例都处理完毕。

        总的来说,这个代码实现了一个处理多个测试用例的程序,每个测试用例都是一个数组操作模拟,最终输出模拟操作后的结果。代码使用了标准库中的 stack 来模拟栈的操作,并使用了 vector 存储输入数组的元素,以及 pair 类型定义的别名 pii 和 ll 类型定义的别名 ll。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值