标签:
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.
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.
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).
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] + " "); } } } } }
FZU 2215 Simple Polynomial Problem (多项式乘法 栈)
标签:
原文地址:http://blog.csdn.net/tc_to_top/article/details/52262954