Problem Statement
We have three types of brackets: "()", "[]", and "{}". We are now interested in some special strings. A string is special if all the following conditions hold:
- Each character of the string is one of the six bracket characters mentioned above.
- The characters of the string can be divided into disjoint pairs such that in each pair we have an opening bracket and a closing bracket of the same type.
- For each pair, the opening bracket must occur to the left of the corresponding closing bracket.
- For each pair, the substring strictly between the opening and the closing bracket must be a special string (again, according to this definition).
For example, the empty string is a special string: there are 0 pairs of brackets. The string "[]" is also a special string: there is one pair of matching brackets, they are in the proper order, and the string between them (which is the empty string) is a special string.
The character 'X' (uppercase x) occurs in expression at most five times; all other characters in expression are brackets of the types mentioned above. We want to changeexpression into a special string by changing each 'X' into one of the brackets. (Different occurrences of 'X' may be changed into different brackets.) Return "possible" (quotes for clarity) if we can do that, and "impossible" otherwise.
Definition
- ClassBracketExpressions
- MethodifPossible
- Parametersstring
- Returnsstring
- Method signaturestring ifPossible(string expression)
Limits
- Time limit (s)2.000
- Memory limit (MB)256
Constraints
- expression will have between 1 and 50 characters, inclusive.
- Each character in expression will be '(', ')', '[', ']', '{', '}' or 'X'.
- There will be at most 5 occurences of 'X' in expression.
Test cases
-
- expression"([]{})"
Returns "possible"This is already a special string. As there are no 'X's, we do not get to change anything. -
- expression"(())[]"
Returns "possible" -
- expression"({])"
Returns "impossible" -
- expression"[]X"
Returns "impossible"Regardless of bracket type you put instead of 'X', you cannot create a special string. -
- expression"([]X()[()]XX}[])X{{}}]"
Returns "possible"You can replace 'X's respectively with '{', '(', ')' and '['.
题意:给小于5个X,可在字符串中替换括号,判断是否是该括号字符串合法
We are looking for a string that is a valid bracket expression of ( ), [ ] and { } brackets. Let's begin with something simpler: How can we verify if a string is a valid bracket expressions? This is a classic problem. We can use recursion and it is also a possible application of stacks. As follows:
bool correctBracket(string exp)
{
stack<char> s;
// an assoicaitive array: opos[ ')' ] returns '(', opos[ ']' ] is '[', ...
map<char,char> opos = {
{ ')', '(' },
{ ']', '[' },
{ '}', '{' },
};
for (char ch: exp) {
// we push opening brackets to the stack
if (ch == '(' || ch == '[' || ch == '{' ) {
s.push(ch);
} else {
// If we find a closing bracket, we make sure it matches the
// opening bracket in the top of the stack
if (s.size() == 0 || s.top() != opos[ch]) {
return false;
} else {
// then we remove it
s.pop();
}
}
}
// stack must be empty.
return s.empty();
}
Filling the blanks
The problem asks us to replace some of the 'X' characters in the input string to make it valid. But the problem setter was kind with us and added a constraint of at most 5 'X' characters. This means that there are not many different ways to fill the blanks with some of the 6 possible characters. For 5 slots, we have 6 options: 65=7776 . The idea we will apply is to use brute force and try every possible combination of characters to fill each X. We can use backtracking. But to make things interesting, here is a different bruteforce approach based on the base-6 encoding of numbers. If there are t X in the input, we can try all numbers from 0 to 6t−1 , each digit in the base-6 representation of a number represents a different character: (, ), [, ], }, {:
string ifPossible(string expression)
{
vector<int> x;
int n = expression.size();
for (int i = 0; i < n; i++) {
if (expression[i] == 'X') {
x.push_back(i);
}
}
int t = x.size();
// to easily convert to base 6 we precalculate the powers of 6:
int p6[6];
p6[0] = 1;
for (int i = 1; i < 6; i++) {
p6[i] = 6 * p6[i - 1];
}
const char* CHARS = "([{)]}";
for (int m = 0; m < p6[t]; m++) {
string nexp = expression;
for (int i = 0; i < t; i++) {
// (m / p6[i]) % 6 extracts the i-th digit of m in base 6.
nexp[ x[i] ] = CHARS[ (m / p6[i]) % 6 ];
}
if (correctBracket(nexp)) {
return "possible";
}
}
return "impossible";
}

1693

被折叠的 条评论
为什么被折叠?



