Simple Polynomial Problem FZU - 2215 (string evaluation + polynomial)

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

Idea: After we already have the algorithm for evaluating strings, it is easy to understand this problem. We can treat each non-operator character in the string as a polynomial, then we can convert this problem into Problems evaluating strings.

For example (1+x)*(1+x) becomes (1+2)*(3+4) where each number represents a polynomial, which is the subscript of the polynomial array.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+500;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

char s[1005];
int a[1005];
int iid[500];
struct mat{
	ll x[501];
};


mat operator + (mat a, mat b) // overloaded polynomial addition
{
	for(int i = 500; i >= 0; i--)
	{
		a.x[i] = (b.x[i]+a.x[i])%mod;
	}
	return a;
}
	
mat operator * (mat a,mat b) // overloaded polynomial multiplication
{
	matte years;
	memset(ans.x,0,sizeof(ans.x));
	int al = 500;
	int bl = 500;
	while(a.x[al] == 0) al--;
	while(b.x[bl] == 0) bl--;
	for(int i = al; i >= 0; i--)
	for(int j = bl; j >= 0; j--)
	{
		ans.x[i+j] += ax[i]*bx[j];
		ans.x [i + j]% = mod;
	}
	return ans;
}

int pri[][6] = { // priority allows array
    0,0,0,0,0,0,
    0,0,0,0,0,0,
    0,0,0,0,0,0,
    0,0,1,0,0,0,
    0,1,1,1,1,0,
    0,1,1,1,1,0
};
void get() // operator number
{
	iid['('] = 1;
	iid['*'] = 2;
	iid['+'] = 3;
	iid[')'] = 4;	
	iid['#'] = 5;	
}
int j;
mat mt[1005];

mat solve(int n) // string evaluation
{
	stack <int> s1;
	stack <int> s2;
	for(int i = 0; i < n; i++)
	{	
		if(a[i] >= 0) // is a number
		{
			s1.push(a[i]);
		}
		else // is the symbol
		{
			while(s2.size() && pri[-a[i]][-(s2.top())])
			{
				int x = s2.top();
				s2.pop();
				if(-x == 1) break;
				if(-x == 2)
				{
					int y = s1.top();
					s1.pop();
					int yy = s1.top();
					s1.pop();
					mt[j] = mt[y]*mt[yy];
					s1.push(j++);
				}
				if(-x == 3)
				{
					int y = s1.top();
					s1.pop();
					int yy = s1.top();
					s1.pop();
					mt[j] = mt[y]+mt[yy];
					s1.push(j++);
				}
			}
			if(a[i] != -4) s2.push(a[i]); // no closing parenthesis
		}
	}
	return mt[s1.top()];
}


intmain()
{
    int t;
    scanf("%d",&t);
    get();
    while(t--)
    {
    	scanf("%s",s);
    	int n = strlen(s);
    	j = 1;
    	memset(mt,0,sizeof(mt));
    	for(int i = 0; s[i]; i++)
    	{
    		if(s[i] >= '0' && s[i] <= '9')
    		{
    			mt [j] .x [0] = s [i]-'0';
    			a[i] = j++;
    		}
    		else if(s[i] == 'x')
    		{
    			mt [j] .x [1] = 1;
    			a[i] = j++;
    		}
    		else
    		a[i] = iid[s[i]]*(-1);
    	}
    	a[n] = -5;
    	int i = 500;
    	mat ans = solve (n + 1);
    	while(i > 0 && ans.x[i] == 0) i--; // note non-zero output
    	for(;i>=0; i--)
    	{
    		printf(i == 0 ?"%I64d\n" : "%I64d ",ans.x[i]);
    	}
    }
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806731&siteId=291194637