*[topcoder]BracketExpressions
http://community.topcoder.com/stat?c=problem_statement&pm=13243
就是能否通过把字符串中的'X'替换成"()", "[]", and "{}"来变成合法的括号字符串,
"([]X()[()]XX}[])X{{}}]"
Returns: "possible"
You can replace 'X's respectively with '{', '(', ')' and '['.
DFS搜索,Valid的判断使用stack。
#include <stack>
#include <vector>
#include <string>
using namespace std; class BracketExpressions {
public:
string candidates; string ifPossible(string expression)
{
candidates = "()[]{}";
vector<int> xPos;
for (int i = 0; i < expression.length(); i++)
{
if (expression[i] == 'X')
{
xPos.push_back(i);
}
}
bool possible = ifPossRe(expression, 0, xPos);
if (possible)
return "possible";
else
return "impossible";
} bool isValid(const string &expression)
{
stack<char> stk;
for (int i = 0; i < expression.length(); i++)
{
if (stk.empty())
{
stk.push(expression[i]);
}
else if (match(stk.top(), expression[i]))
{
stk.pop();
}
else
{
stk.push(expression[i]);
}
}
return stk.empty();
} bool match(char a, char b)
{
if (a == '(' && b == ')') return true;
if (b == '(' && a == ')') return true;
if (a == '[' && b == ']') return true;
if (b == '[' && a == ']') return true;
if (a == '{' && b == '}') return true;
if (b == '{' && a == '}') return true;
return false;
} bool ifPossRe(string &expression, int idx, vector<int> &xPos)
{
if (idx == xPos.size())
{
return isValid(expression);
}
int n = xPos[idx];
for (int i = 0; i < candidates.length(); i++)
{
char ch = expression[n];
expression[n] = candidates[i];
bool res = ifPossRe(expression, idx+1, xPos);
expression[n] = ch;
if (res)
return true;
}
return false;
}
};
http://apps.topcoder.com/wiki/display/tc/SRM+628
但其实判断Backet合法的代码是错的,因为没有判断先有左括号再有右括号,以下做法更好更简洁。
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();
}
解法中还是用了基于6的幂来计算所有组合,比DFS要快。
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";
}
*[topcoder]BracketExpressions的更多相关文章
- topcoder SRM 628 DIV2 BracketExpressions
先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
随机推荐
- 常用JS加密编码算法
//#region UTF8编码函数 function URLEncode(Str) { if (Str == null || Str == "") return "&q ...
- WPF中XAML转义字符
字符 转义字符 备注 & (ampersand) & 这个没什么特别的,几乎所有的地方都需要使用转义字符 > (greater-than character) > 在属性( ...
- 重拾C,一天一点点_5
switch(表达式){ case 整型常量表达式:语句序列 case 整型常量表达式:语句序列 default:语句序列} while(表达式) 语句 for(表达式1; 表 ...
- PHP操作MongoDB简明教程(转)
转自:http://blog.sina.com.cn/s/blog_6324c2380100ux2m.html MongoDB是最近比较流行的NoSQL数据库,网络上关于PHP操作MongoDB的资料 ...
- C#中窗体的互相访问
1.在父窗体中构造子窗体对象时,将父窗体传递过去: 如:FrmSub frm=new FrmSub(this);//this代表父窗体 2.将父窗体中要访问的变量和方法修改为public 3.在子窗体 ...
- rtx信息泄漏利结合弱口令导致被批量社工思路
腾讯通RTX(Real Time eXchange)是腾讯公司推出的企业级实时通信平台. rtx server 存在暴露用户信息的漏洞,通过web访问 http://RtxServerIp:8012/ ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- iOS学习笔记之typedef
typedef unsigned long long weiboId; typedef 定义一个使用方便的类型,谓之为“宏定义“. unsigned long long 是一种无符号的长长整型.本应该 ...
- 如何在mysql中退出当前窗口界面而不关闭窗口的方法
CTRL+Cexit;quit ;
- JPA学习---第八节:使用JPQL语句进行查询
1.JPQL 语句查询,代码如下: @Test public void query(){ EntityManagerFactory factory = Persistence.createEntity ...