【FZU 2215】Simple Polynomial Problem(后缀表达式+栈的应用)

D - Simple Polynomial Problem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

You are given an polynomial of x consisting of only addition marks, multiplication marks, brackets, single digit numbers, and of course the letter x. For example, a valid polynomial would be: (1+x)*(1+x*x+x+5)+1*x*x.

You are required to write the polynomial into it's minimal form by combining the equal terms.

For example, (1+x)*(1+x) would be written as x^2+2*x+1.

Also, (1+x)*(1+x*x+x+5)+1*x*x would be written as x^3+3*x^2+7*x+6.

Input

The first line contains an integer T, meaning the number of the cases. 1 <= T <= 50.

For each test case, there will be one polynomial which it's length would be not larger than 1000.

It is guaranteed that every input polynomial is valid, and every number would be a single digit.

Output

For each polynomial, output it's minimal form. If the polynomial's minimal form has n terms, output n space-separated integers.

You only have to output each term's corresponding constant (from highest to lowest). In case the constant gets too big, output the remainder of dividing the answer by 1000000007 (1e9 + 7).

Sample Input

41*(1+1*(1+1))(1+x)*(1+x)x*((1+x)*(1+x*x+x+5)+1*x*x)(x*x*x*0+x*2)*x

Sample Output

31 2 11 3 7 6 02 0 0



题意:给你一个只有+, *, 数字和字母构成的多项式,求完全展开以后的各项系数。

思路:模拟题,和表达式求值差不多,新建一个多项式结构体进行运算即可,代码中有些细节注意处理。

ps:我的代码在visual c++编译器下ac,但是在gnu c++下是re。



#include <cstdio>
#include <iostream>
#include <cmath>
#include <stack>
#include <algorithm>
#include <cstring>
#define MOD 1000000007
using namespace std;
struct Poly {
    long long a[1050];
    Poly() {
        memset(a,0,sizeof(a));
    }
    Poly operator+(const Poly &p) {                    ///重载+运算,实现多项式的加法
        Poly res;
        for(int i=0; i<1050; i++) {
            res.a[i]=(a[i]+p.a[i])%MOD;
        }
        return res;
    }
    Poly operator*(const Poly &p) {                     ///重载*运算,实现多项式乘法
        Poly res;
        for(int i=0; i<1050; i++) {
            if(a[i]==0)
                continue;
            for(int j=0; j<1050; j++) {
                if(p.a[j]==0)
                    continue;
                res.a[i+j]+=a[i]*p.a[j];
                res.a[i+j]%=MOD;
            }
        }
        return res;
    }
};
stack<Poly>sp;                                  ///用于后缀表达式求值
stack<char>sc;                                  ///用于中缀表达式转后缀
int check(char x) {                             ///判断符号优先级
    if(x=='(') return 0;
    if(x=='+'||x=='*') return 1;
    if(x=='x') return 2;
    if(x>='0'&&x<='9') return 3;
    if(x==')') return 4;
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        while(!sp.empty())
            sp.pop();
        char p[1050],tmp[1050];
        memset(tmp,'',sizeof(tmp));
        scanf("%s",p);
        int len=strlen(p),cnt=0;
        for(int i=0; i<len; i++) {
            if(check(p[i])==0)
                sc.push(p[i]);
            else if(check(p[i])==1) {
                while(!sc.empty()&&sc.top()=='*'&&p[i]=='+') {
                    tmp[cnt++]=sc.top();                    ///注意当栈顶是*,i位是+时要弹出栈顶元素,再入栈
                    sc.pop();
                }
                sc.push(p[i]);
            } else if(check(p[i])==2)
                tmp[cnt++]=p[i];
            else if(check(p[i])==3) {
                if(check(p[i+1])==3)
                    tmp[cnt++]=p[i];
                else {
                    tmp[cnt++]=p[i];                    ///在一个数字结束加一个#为了区分不同的常数
                    tmp[cnt++]='#';
                }
            } else if(check(p[i])==4) {
                while(sc.top()!='(') {
                    tmp[cnt++]=sc.top();
                    sc.pop();
                }
                sc.pop();
            }
        }
        while(!sc.empty()) {
            tmp[cnt++]=sc.top();
            sc.pop();
        }
        int maxcs=0;
        long long ans=0;
        for(int i=0; i<cnt; i++) {
            Poly tt;
            if(check(tmp[i])==2) {
                tt.a[1]=1;
                sp.push(tt);
                maxcs=1;
            } else if(check(tmp[i])==3) {
                while(check(tmp[i])==3) {
                    ans=ans*10+tmp[i]-'0';
                    i++;
                }
                tt.a[0]=ans;
                sp.push(tt);
                ans=0;
            } else if(check(tmp[i])==1) {
                Poly t1=sp.top();
                sp.pop();
                Poly t2=sp.top();
                sp.pop();
                if(tmp[i]=='+')
                    sp.push(t1+t2);
                else if(tmp[i]=='*')
                    sp.push(t1*t2);
            }
        }
        Poly res=sp.top();
        int id=-1;                               ///存第一个不是0的项数的下标,即多项式展开后的最高次项数
        for(int i=1049; i>=0; i--) {
            if(res.a[i]) {
                id=i;
                break;
            }
        }
        if(id!=-1) {
            for(int i=id; i>=0; i--) {
                printf("%I64d",res.a[i]);
                printf(i==0?"
":" ");
            }
        }
        else printf("0
");
    }
}





原文地址:https://www.cnblogs.com/Torrance/p/5410559.html