FZU 2215 Simple Polynomial Problem【模拟】【表达式计算】

来源:互联网 发布:tensorflow.google.cn 编辑:程序博客网 时间:2024/05/14 17:50

题目链接

http://acm.fzu.edu.cn/problem.php?pid=2215

思路

题意就是叫你把给你的多项式化到最简,输出各项系数。

表达式计算用栈实现就行了,但这题需要改动下,数据栈内不能直接存数字了,而要存多项式,定义一个数组c[i]表示xi 的系数,然后自己实现下加法乘法即可。

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
原创粉丝点击