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] + " "); } } } } }