https://practice.geeksforgeeks.org/problems/minimum-swaps-for-bracket-balancing/0

You are given a string of 2N characters consisting of N ‘[‘ brackets and N ‘]’ brackets. A string is considered balanced if it can be represented in the for S2[S1] where S1 and S2 are balanced strings. We can make an unbalanced string balanced by swapping adjacent characters. Calculate the minimum number of swaps necessary to make a string balanced.

Input:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the length of the string.

The second line of each test case contains the string consisting of ‘[‘ and ‘]’.

Output:

Print the minimum number of swaps to make the string balanced for each test case in a new line.

Example:
Input : []][][
Output : 2
First swap: Position 3 and 4
[][]][
Second swap: Position 5 and 6
[][][]
Input : [[][]]
Output : 0
String is already balanced.

方法一:每碰到一个 ‘ ] ‘ ,抵消左侧的一个 ‘ [ ‘ ,如果左侧没有 ,则抵消右侧的,此时需要要 swap ,sum += pos(‘ [ ‘) -pos (‘ ] ‘)。但是此时 应回到交换后的 ‘ [ ‘ 重新开始,因为可能已影响到交换前 ‘ ] ‘ 之后符号的决策。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
int n,t;
string s;
// Function to calculate swaps required
long swapCount(string s)
{
// Keep track of '['
vector<int> pos;
for (int i = 0; i < s.length(); ++i)
if (s[i] == '[')
pos.push_back(i);

int count = 0; // To count number of encountered '['
int p = 0; // To track position of next '[' in pos
long sum = 0; // To store result

for (int i = 0; i < s.length(); ++i)
{
// Increment count and move p to next position
if (s[i] == '[')
{
++count;
++p;
}
else if (s[i] == ']')
--count;

// We have encountered an unbalanced part of string
if (count < 0)
{
// Increment sum by number of swaps required
// i.e. position of next '[' - current position
sum += pos[p] - i;
swap(s[i], s[pos[p]]);
++p;

// Reset count to 1
count = 1;
}
}
return sum;
}
int main(){
freopen("in","r",stdin);
cin>>t;
while(t--){
cin>>n;
cin>>s;
cout<<swapCount(s)<<endl;
}
return 0;
}

方法二:记录 ‘ ] ‘ 不匹配的个数 imbalance,遇见 ‘ [ ‘ 时,优先配对最远的 ‘ ] ‘,且 imbalance –,依序往下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
const int MAX=100000+10;
int n,t;
string s;
long swapCount(string s)
{
// stores total number of Left and Right
// brackets encountered
int countLeft = 0, countRight = 0;
// swap stores the number of swaps required
//imbalance maintains the number of imbalance pair
int swap = 0 , imbalance = 0;

for(int i =0; i< s.length(); i++)
{
if(s[i] == '[')
{
// increment count of Left bracket
countLeft++;
if(imbalance > 0)
{
// swaps count is last swap count + total
// number imbalanced brackets
swap += imbalance;
// imbalance decremented by 1 as it solved
// only one imbalance of Left and Right
imbalance--;
}
} else if(s[i] == ']' )
{
// increment count of Right bracket
countRight++;
// imbalance is reset to current difference
// between Left and Right brackets
imbalance = (countRight-countLeft);
}
}
return swap;
}
int main(){
freopen("in","r",stdin);
cin>>t;
while(t--){
cin>>n;
cin>>s;
cout<<swapCount(s)<<endl;
}
return 0;
}