*[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 ...
随机推荐
- net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转)
作者:王先荣前言 随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提供了VFW.DirectShow和MediaFoundation这 三代接口.其中VFW早已被Di ...
- Android混淆问题
最近做了2个项目,全部要混淆,刚接触,自己在网上找了还多资料,感觉各有千秋,自己总结了一下,第一次发帖,不喜勿喷.求各种指导!!! android应用程序的混淆打包规范 1.在工程文件project. ...
- 用PHP判断客户端是否是手机
<?php function isMobile(){ $useragent = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_A ...
- 微软职位内部推荐-Android Developer
微软近期Open的职位: Position: SDE II or Senior SDE -- Mobile Products Android/WP Contact Person: Winnie Wei ...
- Payment Terms 收付款条件和分期付款设置
SAP Payment Terms 中文翻译为收付款条件,他的用途是应收和应付的财务凭证中账期的管理,顾名思义即手动录入和自动生成的财务文档多少天内冲销处理则为正常,否则为超期应收应付文档,它包含的内 ...
- LintCode-Hash Function
In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...
- Oracle 多行记录合并/连接/聚合字符串的几种方法
怎么合并多行记录的字符串,一直是oracle新手喜欢问的SQL问题之一,关于这个问题的帖子我看过不下30个了,现在就对这个问题,进行一个总结.-什么是合并多行字符串(连接字符串)呢,例如: SQL&g ...
- SQL Server 2008之数据库大型应用解决方案总结
着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互联网应用,每天百万级甚至上亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的问题. 一. ...
- Unity3D 利用NGUI2.6.3做技能冷却的CD效果
转自http://blog.csdn.net/qqmcy/article/details/9469021 NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件 ...
- iOS Core data多线程并发访问的问题
大家都知道Core data本身并不是一个并发安全的架构:不过针对多线程访问带来的问题,Apple给出了很多指导:同时很多第三方的开发者也贡献了很多解决方法.不过最近碰到的一个问题很奇怪,觉得有一定的 ...