编程介的小学生 2018-12-31 22:32 采纳率: 20.5%
浏览 360
已采纳

一个括号匹配的问题,是不是堆栈的实现?除堆栈还可以怎么实现,C语言?

Problem Description
Miceren likes playing with brackets.

There are N brackets on his desk forming a sequence. In his spare time, he will do Q operations on this sequence, each operation is either of these two types:

  1. Flip the X-th bracket, i.e. the X-th bracket will become ) if it is ( before, otherwise become ( .

  2. In a given subsequence [L, R], query the K-th bracket position number after deleting all matched brackets. If the amount of brackets left is less than K, output -1.
    For example, if N = 10, L = 2, R = 9 and the sequence is ))(()))((). In the sub sequence [2, 9], the brackets sequence is )(()))((. After deleting all matched brackets, the sequence becomes ) )((. If K = 1, answer is 2. If K = 2, answer is 7. If K = 3, answer is 8. If K = 4, answer is 9. If K ≥ 5, answer is -1.

Miceren is good at playing brackets, instead of calculating them by himself.

As his friend, can you help him?

Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begins with two integers N, Q, indicating the number of brackets in Miceren's desk and the number of queries.

Each of following Q lines describes an operation: if it is "1 X", it indicate the first type of operation. Otherwise it will be "2 L R K", indicating the second type of operation.

T is about 100.

1 ≤ N,Q ≤ 200000.

For each query, 1 ≤ X ≤ N and 1 ≤ L ≤ R ≤ N, 1 ≤ K ≤ N.

The ratio of test cases with N > 100000 is less than 10%.

Output
For each query operation, output the answer. If there is no K brackets left, output -1.

Sample Input
1
10 5
))(()))(()
2 2 9 2
2 2 9 3
2 2 9 5
1 3
2 2 9 5

Sample Output
7
8
-1
8

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-10-04 08:02
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了