FZU 2215 Simple Polynomial Problem【模拟】【表达式计算】
来源:互联网 发布:tensorflow.google.cn 编辑:程序博客网 时间:2024/05/14 17:50
题目链接
http://acm.fzu.edu.cn/problem.php?pid=2215
思路
题意就是叫你把给你的多项式化到最简,输出各项系数。
表达式计算用栈实现就行了,但这题需要改动下,数据栈内不能直接存数字了,而要存多项式,定义一个数组c[i]表示
AC代码
#include <iostream>#include <cmath>#include <stack>#include <string>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int MOD = 1000000007;struct polynomial{ ll c[600]; int n; polynomial() { n = 0; memset(c, 0, sizeof c); } friend polynomial operator + (polynomial a, polynomial b) { polynomial ans; int len = max(a.n, b.n); for (int i = 0; i <= len; ++i) { ans.c[i] = a.c[i] + b.c[i]; ans.c[i] %= MOD; } ans.n = len; return ans; } friend polynomial operator * (polynomial a, polynomial b) { polynomial ans; if (a.c[a.n] == 0 || b.c[b.n] == 0) return ans; for (int i = 0; i <= a.n; ++i) { if (i != 0)//每次向右移动一位 { for (int j = b.n + 1; j >= 1; --j) { b.c[j] = b.c[j - 1]; } b.c[0] = 0; b.n++; } polynomial t = b; for (int j = 0; j <= b.n; ++j) { t.c[j] *= a.c[i]; t.c[j] %= MOD; } ans = ans + t; } return ans; }};stack <polynomial> st_num;stack <char> st_op;void calAB(char op){ if (op == '+') { st_op.pop(); polynomial a, b; a = st_num.top(); st_num.pop(); b = st_num.top(); st_num.pop(); st_num.push(a + b); } else if (op == '*') { st_op.pop(); polynomial a, b; a = st_num.top(); st_num.pop(); b = st_num.top(); st_num.pop(); st_num.push(a * b); }}polynomial calculate(string s){ for (int i = 0; i < s.length(); ++i) { if (s[i] == '(' || s[i] == '*') { st_op.push(s[i]); } else if (s[i] == '+') { while (!st_op.empty()) { if (st_op.top() == '*') { calAB('*'); } else break; } st_op.push(s[i]); } else if (s[i] == ')') { while (!st_op.empty()) { if (st_op.top() == '*') { calAB('*'); } else if (st_op.top() == '+') { calAB('+'); } else if (st_op.top() == '(') { st_op.pop(); break; } } } else if (isdigit(s[i])) { polynomial t; t.n = 0; t.c[0] = s[i] - '0'; st_num.push(t); } else if (s[i] == 'x') { polynomial t; t.n = 1; t.c[1] = 1; st_num.push(t); } } while (!st_op.empty())//最后清算 { if (st_op.top() == '*') { calAB('*'); } else if (st_op.top() == '+') { calAB('+'); } } polynomial ans = st_num.top(); while (!st_num.empty())st_num.pop(); while (!st_op.empty())st_op.pop(); return ans;}int main(){ int T; scanf("%d", &T); while (T--) { string s; cin >> s; polynomial ans = calculate(s); bool flag = 0; for (int i = ans.n; i >= 0; --i) { if (i == ans.n)printf("%lld", ans.c[i]); else printf(" %lld", ans.c[i]); } printf("\n"); } return 0;}
0 0
- FZU 2215 Simple Polynomial Problem【模拟】【表达式计算】
- FZU 2215 Simple Polynomial Problem 多项式模拟 表达式树
- 【FZU 2215】Simple Polynomial Problem
- FZU 2215 Simple Polynomial Problem (多项式乘法 栈)
- 表达式树+多项式模拟 fzu2215 Simple Polynomial Problem
- 【模拟题、表达式求值】hdu hdoj 1296 Polynomial Problem
- Simple String Problem FZU
- FZU 1350 Very Simple Problem
- FZU - 2218 Simple String Problem
- FZU 2218 Simple String Problem
- FZU-2218 Simple String Problem
- FZU 2218 Simple String Problem
- FZU 2218 Simple String Problem
- Polynomial Problem(hdu 1296 表达式求值)
- FZU 2218 Simple String Problem (状压dp)
- FZU 2218 Simple String Problem(状压DP)
- HDU2522 A simple problem 【模拟】
- FZU 2108 Mod problem 数学与模拟
- ACM-2004
- js求两者时间差
- sharding-jdbc使用
- Ceph部署时提示:/etc/init.d/ceph: line 15: /lib/lsb/init-functions: No such file or directory
- 关于 ScrollLayout加载时总是显示在中间的视图解决方法
- FZU 2215 Simple Polynomial Problem【模拟】【表达式计算】
- 通过JUnit进行Android单元测试
- 离散化黑科技
- 【bzoj2243】[SDOI2011]染色 树链剖分 (区间合并处理)
- ACM-2005
- Lightoj1008——Fibsieve`s Fantabulous Birthday(找规律)
- hdoj 1016 dfs(素数环)
- CSS3.0使用@font-face自定义字体
- StringUtils的isBlank与isEmply