Codeforces Round #653 (Div. 3) C. Move Brackets(思维)

Move Brackets:

题目大意:(文末有原题)

给出一个由 ')' 和 '(' 组成的字符串,我们可以将一个字符移动到字符串首或尾,输出最少通过多少次移动可以使每个')' 前都有与它匹配的 '(';

匹配规则:

() 是对的

(()) 也是对的

(()()) 也是对的

)( 是错的

思路:

因为要构成对,只需把 '(' 移动到 ')' 之前,所以说每有一个不成对的括号,就需要移动一次,所以只需判断有多少个还没有成对;

代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
	int t;
	cin >> t;
	while(t--) {
		int n;
		int vis[1000];
		memset(vis, 0, sizeof(vis));
		cin >> n;
		char s[1000];
		
		for(int i = 0; i < n; i++) {	//将'('')'用数字表示;('-') 
			char c;
			cin >> c;
			if(c == '(') c = '1';
			if(c == ')') c = '0';
			s[i] = c;
		}
		
		int m = n / 2;	//m表示总对数 
		
		for(int i = n - 1; i >= 1; i--) {
			if(s[i] == '1') continue;
			for(int j = i - 1; j >= 0; j--) {	//查找这个')'之前有没有'(' 
				if(vis[j] == 0 && s[j] == '1') {
					m--;
					vis[j]++;
					vis[i]++;
					break;
				}
			}
			if(vis[i]) continue; //[i]没有被标记,则说明它之前没有'('了,剩下的还没有匹配对就是需要移动的步数; 
			else break;
		}
		
		cout << m << endl;
	}
	
	return 0;
}

原题:

题目:

You are given a bracket sequence s of length n, where n is even (divisible by two). The string s consists of n/2 opening brackets '(' and n/2 closing brackets ')'.

In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index i, remove the i-th character of ss and insert it before or after all remaining characters of s).

Your task is to find the minimum number of moves required to obtain regular bracket sequence from s. It can be proved that the answer always exists under the given constraints.

Recall what the regular bracket sequence is:

  • "()" is regular bracket sequence;
  • if s is regular bracket sequence then "(" + s + ")" is regular bracket sequence;
  • if s and t are regular bracket sequences then s + t is regular bracket sequence.

For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.

You have to answer t independent test cases.

输入:

The first line of the input contains one integer t (1≤t≤2000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (2≤n≤50) — the length of ss. It is guaranteed that n is even. The second line of the test case containg the string s consisting of n/2 opening and n/2 closing brackets.

输出:

For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from s. It can be proved that the answer always exists under the given constraints.

样例:

Input:        Output:

4
2
)( ------------------ 1
4
()() ---------------- 0
8
())()()( ------------ 1
10
)))((((()) ---------- 3

Note

In the first test case of the example, it is sufficient to move the first bracket to the end of the string.

In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.

In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())".

©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值