The North American Invitational Programming Contest 2017 题目
Yin and Yang Stones
- 75.39%
- 1000ms
- 262144K
A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain.
Ming has two operations for balancing the stones:
- Take some consecutive sequence of stones where there is exactly one more black stone than a white stone and replace the stones with a single black stone
- Take some consecutive sequence of stones where there is exactly one more white stone than black stone and replace the stones with a single white stone
Given a circular arrangement, determine if it is possible for Ming to balance the stones.
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The input will consist of a single string sss (1≤∣s∣≤105)(1 \le |s| \le 10^5)(1≤∣s∣≤105), with only the characters capital ‘BBB’ and ‘WWW’. The stones are arranged in a circle, so the first stone and the last stone are adjacent.
Output
Output 111 if it is possible for Ming to balance the stones with his rules. Otherwise, output 000.
样例输入1
WWBWBB
样例输出1
1
样例输入2
WWWWBBW
样例输出2
0
样例输入3
WBBBBBWWBW
样例输出3
0
题目来源
The North American Invitational Programming Contest 2017
思路:W与B相同输出1,否则输出0。只有这样才能保持黑白平衡。
Pieces of Parentheses
- 22.03%
- 1000ms
- 262144K
You are teaching a class in programming, and you want to cover balanced parentheses. You’ve got a great visual aid, a sign with a very long, balanced string of parentheses. But, alas, somehow, your visual aid has been broken into pieces, and some pieces may be missing! You’ve got to try to put it back together as best you can. Given the string of parentheses on each piece, what is the longest balanced string you can form by concatenating some of them in some order? Each piece may be used at most once, and the pieces cannot be reversed.
A balanced string of parentheses is defined as:
- The empty string
- ABABAB where AAA and BBB are both balanced strings of parentheses
- (A)(A)(A) where AAA is a balanced string of parentheses
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain a single integer n(1≤n≤300)n (1 \le n \le 300)n(1≤n≤300), which is the number of pieces.
Each of the next nnn lines will hold a single string s(1≤∣s∣≤300)s (1 \le |s| \le 300)s(1≤∣s∣≤300), which consists only of the characters ’(((’ and ’)))’. This describes one of the pieces.
Output
Output a single integer, which is the length of the longest string of balanced parentheses you can form from the pieces. Note that the empty string is technically a balanced string of parentheses, so it is always possible to form a string of length at least 000 (although the empty string is not a very effective visual aid!).
样例输入1
3 ()) ((() )()
样例输出1
10
样例输入2
5 ))))) ) (( ))(( (
样例输出2
2
题目来源
The North American Invitational Programming Contest 2017
代码:
1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 template <class T, class C> 6 using heap = priority_queue<T, vector<T>, C>; 7 void abc(string s, int &a, int &b, int &c) 8 { 9 a = 0, 10 b = 0, 11 c = s.length(); 12 for (int i = 0; i < s.length(); i++) 13 { 14 switch (s[i]) 15 { 16 case '(': 17 a++; 18 break; 19 case ')': 20 if (a > 0) 21 { 22 a--; 23 } 24 else 25 { 26 b++; 27 } 28 } 29 } 30 } 31 struct triple 32 { 33 int a, 34 b, 35 c; 36 }; 37 bool operator>(const triple &A, const triple &B) 38 { 39 if (A.b ^ B.b) 40 { 41 return A.b > B.b; 42 } 43 if (A.a ^ B.a) 44 { 45 return A.a < B.a; 46 } 47 return A.c < B.c; 48 } 49 bool operator<(const triple &A, const triple &B) 50 { 51 if (A.a ^ B.a) 52 { 53 return A.a > B.a; 54 } 55 if (A.b ^ B.b) 56 { 57 return A.b < B.b; 58 } 59 return A.c < B.c; 60 } 61 int main() 62 { 63 int n{0}; 64 cin >> n; 65 int A[90001], B[90001]; 66 memset(A, 0xf0, sizeof(A)); 67 memset(B, 0xf0, sizeof(B)); 68 A[0] = 0; 69 B[0] = 0; 70 heap<triple, greater<triple>> I; 71 heap<triple, less<triple>> D; 72 for (int i = 1; i <= n; i++) 73 { 74 string s; 75 cin >> s; 76 int a{0}, b{0}, c{0}; 77 abc(s, a, b, c); 78 if (a >= b) 79 { 80 I.push({a, b, c}); 81 } 82 else 83 { 84 D.push({a, b, c}); 85 } 86 } 87 while (I.size()) 88 { 89 const int a = I.top().a, 90 b = I.top().b, 91 c = I.top().c; 92 for (int x = 90000; x >= max(b, a - b); x--) 93 { 94 A[x] = max(A[x], A[x - a + b] + c); 95 } 96 I.pop(); 97 } 98 while (D.size()) 99 { 100 const int a = D.top().a, 101 b = D.top().b, 102 c = D.top().c; 103 for (int x = 90000; x >= max(a, b - a); x--) 104 { 105 B[x] = max(B[x], B[x - b + a] + c); 106 } 107 D.pop(); 108 } 109 int reponse{0}; 110 for (int x = 0; x <= 90000; x++) 111 { 112 reponse = max(reponse, A[x] + B[x]); 113 } 114 cout << reponse << endl; 115 return 0; 116 }
参考博客:http://www.cnblogs.com/JebediahKerman/p/9742462.html