poj2269 Friends
计算表达式。
只有3种运算符:*,+,- ,
*优先级高于后两者,后两者优先级相同。
有两种符号:{},()。
利用递归和堆栈即可解决。
首先遇到左括号开始入栈直到遇到右括号,遇到右括号时对括号内的数进行计算。
考虑到*优先级较高,因此遇到*直接对其左右集合进行运算。
最后得到不含括号和*的表达式,从左往右计算即可。
http://poj.org/problem?id=2269
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = ;
const int maxc = ;
char buf[maxn];
int stack[maxn][maxc];
char op[maxn];
int p, k;
int vis[maxc]; void caculate(){
//meeting eol or right paren RETURN
if(buf[p] == '\0' || buf[p] == ')') return;
if(buf[p] == '('){
int k1 = k;
++p, caculate();
//guarantee that all contents in the paren computed
//figure out the current result
for(int i = k1 + ; i < k; i++){
memset(vis, , sizeof vis);
int o = op[i] == '+' ? : -;
for(int j = ; j <= stack[k1][]; j++) ++vis[stack[k1][j]];
for(int j = ; j <= stack[i][]; j++) vis[stack[i][j]] += o;
stack[k1][] = ;
for(int j = ; j < ; j++) if(vis[j] > ) stack[k1][++stack[k1][]] = j;
}
k = k1 + ;
++p, caculate();
}
else if(buf[p] == '{'){
++p;
stack[k][] = ;
for(int i = p; buf[i] != '}'; i++, ++p) stack[k][++stack[k][]] = buf[i] - 'A';
k++;
p++;
caculate();
}else if(buf[p] == '*'){
if(buf[p + ] == '(') ++p, caculate();
else if(buf[p + ] == '{'){
++p;
stack[k][] = ;
for(int i = p; buf[i] != '}'; i++, ++p) stack[k][++stack[k][]] = buf[i] - 'A';
k++;
++p;
}
//caculate stack[k - 2] * stack[k - 1] to update stack[k - 2]
memset(vis, , sizeof vis);
for(int i = ; i <= stack[k - ][]; i++) ++vis[stack[k - ][i]];
for(int i = ; i <= stack[k - ][]; i++) ++vis[stack[k - ][i]];
stack[k - ][] = ;
for(int i = ; i < ; i++) if(vis[i] == ) stack[k - ][++stack[k - ][]] = i;
--k;
caculate();
}else{
op[k] = buf[p];
++p;
caculate();
}
} void solve(){
k = p = ;
memset(vis, , sizeof );
caculate();
for(int i = ; i < k; i++){
memset(vis, , sizeof vis);
int o = op[i] == '+' ? : -;
for(int j = ; j <= stack[i - ][]; j++) ++vis[stack[i - ][j]];
for(int j = ; j <= stack[i][]; j++) vis[stack[i][j]] += o;
stack[i][] = ;
for(int j = ; j < ; j++) if(vis[j] > ) stack[i][++stack[i][]] = j;
}
putchar('{');
for(int i = ; i <= stack[k - ][]; i++) putchar(stack[k - ][i] + 'A');
putchar('}');
putchar('\n');
} int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_Judge
while(gets(buf) && buf[] != '\0') solve();
return ;
}
poj2269 Friends的更多相关文章
随机推荐
- web app 自适应 弹性布局之rem
关于rem,主要参考文档 1.腾讯ISUX (http://isux.tencent.com/web-app-rem.html) 2.http://www.w3cplus.com/css3/defin ...
- ADO.net 防止SQL 字符串注入攻击
规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...
- weiphp---------图灵机器人存在的bug。
1.很多人下载下来weiphp源码以后,配置好了图灵机器人却不能使用.原因是因为他源码里面存在一个小bug 上图红色框框内是他的源码,问题就出在这里. 修改方法: if($result ['code' ...
- 五种常见的电子商务模式对比:B2B、B2C、C2B、C2C、O2O
电子商务模式是指企业运用互联网开展经营取得营业收入的基本方式,也就是指在网络环境中基于一定技术基础的商务运作方式和盈利模式.目前,常见的电子商务模式主要有B2B.B2C.C2B.C2C.O2O等几种, ...
- 夺命雷公狗ThinkPHP项目之----企业网站2之数据库的快速设计
我们在一个项目的时候,花费最多事件的估计还是数据库的时间了,我们的数据库暂时就这样设计好了: 暂时我们的数据库就这样设计好了用下先,建好后如下所示:
- Python 入門語法和類型(转载学习)
http://www.cnblogs.com/mcdou/archive/2011/08/02/2125016.html Python的设计目标之一是让源代码具备高度的可读性.它设计时尽量使用其它语言 ...
- [Ubuntu] Remove Byte Order Mark (BOM) from files recursively [Forward article]
Original article: http://www.yiiframework.com/wiki/570/remove-byte-order-mark-bom-from-files-recursi ...
- Android SDK Manager更新报错
错误log: Fetching https://dl-ssl.google.com/android/repository/addons_list-.xml Fetched Add-ons List s ...
- 认识Swift
Swift 是一门新的编程语言,用于编写 iOS 和 OS X 应用程序.Swift 结合了 C 和 Objective-C 的优点并且不受C兼容性的限制.Swift 使用安全的编程模式并添加了很多新 ...
- angular源码分析 摘抄 王大鹏 博客 directive指令及系列
链接地址:http://www.cnblogs.com/web2-developer/p/angular-14.html $compile的功能:将一个html字符串或者一个DOM进行编译,返回一个链 ...