FZU 2215 Simple Polynomial Problem (多项式乘法 栈)

129 篇文章 0 订阅
77 篇文章 8 订阅
Problem 2215 Simple Polynomial Problem

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem 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

4
1*(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

3
1 2 1
1 3 7 6 0
2 0 0

Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2215

题目大意:求表达式的结果的多项式的系数

题目分析:直接用栈模拟,记录每个多项式的最大幂指数,乘法的时候一位一位乘然后相加

import java.util.Scanner;
import java.util.Stack;

/**
 * Created by tcgogogo on 16/8/20.
 */

public class Main {

    static class Polynomial {
        public int num;
        public long coef[] = new long[1005];

        public static Polynomial add(Polynomial a, Polynomial b) {
            Polynomial ans = new Polynomial();
            int Max = Math.max(a.num, b.num);
            for(int i = 0; i <= Max; i++) {
                ans.coef[i] = a.coef[i] + b.coef[i];
                ans.coef[i] %= 1000000007;
            }
            ans.num = Max;
            return ans;
        }

        public static Polynomial mul(Polynomial a, Polynomial b) {
            Polynomial ans = new Polynomial();
            if(a.coef[a.num] == 0 || b.coef[b.num] == 0) {
                return ans;
            }
            Polynomial tmp = new Polynomial();
            for(int i = 0; i <= a.num; i++) {
                if(i > 0) {
                    for(int j = b.num + 1; j >= 1; j--) {
                        b.coef[j] = b.coef[j - 1];
                    }
                    b.coef[0] = 0;
                    b.num ++;
                }
                tmp.num = b.num;
                for(int k = 0; k < b.coef.length; k++) {
                    tmp.coef[k] = b.coef[k];
                }
                for(int j = 0; j <= b.num; j++) {
                    tmp.coef[j] *= a.coef[i];
                    tmp.coef[j] %= 1000000007;
                }
                ans = add(ans, tmp);
            }
            return ans;
        }
    }
    public static Stack<Polynomial> stkPolynomial = new Stack<Polynomial>();
    public static Stack<Character> stkOperator = new Stack<Character>();

    public static void Calculate(char operator) {
        if(operator == '+' || operator == '*') {
            stkOperator.pop();
            Polynomial a = stkPolynomial.peek();
            stkPolynomial.pop();
            Polynomial b = stkPolynomial.peek();
            stkPolynomial.pop();
            if (operator == '+') {
                stkPolynomial.push(Polynomial.add(a, b));
            } else {
                stkPolynomial.push(Polynomial.mul(a, b));
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int ca = 1; ca <= T; ca ++) {
            stkPolynomial.clear();
            stkOperator.clear();
            String str = "";
            str = in.next();
            int len = str.length();
            for(int i = 0; i < len; i++) {
                if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                    Polynomial tmp = new Polynomial();
                    tmp.num = 0;
                    tmp.coef[0] = str.charAt(i) - '0';
                    stkPolynomial.push(tmp);
                }
                else if(str.charAt(i) == 'x') {
                    Polynomial tmp = new Polynomial();
                    tmp.num = 1;
                    tmp.coef[1] = 1;
                    stkPolynomial.push(tmp);
                }
                else if(str.charAt(i) == '(') {
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == '*') {
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == '+') {
                    while(!stkOperator.empty()) {
                        if(stkOperator.peek() == '*') {
                            Calculate('*');
                        }
                        else {
                            break;
                        }
                    }
                    stkOperator.push(str.charAt(i));
                }
                else if(str.charAt(i) == ')') {
                    while(!stkOperator.empty()) {
                        if(stkOperator.peek() == '+') {
                            Calculate('+');
                        }
                        else if(stkOperator.peek() == '*') {
                            Calculate('*');
                        }
                        else if(stkOperator.peek() == '(') {
                            stkOperator.pop();
                            break;
                        }
                    }
                }
            }
            while(!stkOperator.empty()) {
                if(stkOperator.peek() == '+') {
                    Calculate('+');
                }
                else if(stkOperator.peek() == '*') {
                    Calculate('*');
                }
            }
            Polynomial ans = stkPolynomial.peek();
            for(int i = ans.num; i >= 0; i--) {
                if(i == 0) {
                    System.out.println(ans.coef[i]);
                }
                else {
                    System.out.print(ans.coef[i] + " ");
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值