# Simple Polynomial Problem FZU - 2215 (字符串求值+多项式)

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

#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)    // 重载多项式加法
{
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)   // 重载多项式乘
{
mat ans;
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] += a.x[i]*b.x[j];
ans.x[i+j] %= mod;
}
return ans;
}

int pri[][6] = {      // 优先级允许数组
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()     // 运算符编号
{
iid['('] = 1;
iid['*'] = 2;
iid['+'] = 3;
iid[')'] = 4;
iid['#'] = 5;
}
int j;
mat mt[1005];

mat solve(int n)   // 字符串求值
{
stack <int> s1;
stack <int> s2;
for(int i = 0; i < n; i++)
{
if(a[i] >= 0)    // 是数字
{
s1.push(a[i]);
}
else    // 是符号
{
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]); // 右括号不加
}
}
return mt[s1.top()];
}

int main()
{
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--;   // 注意非零输出
for(;i>=0; i--)
{
printf(i == 0 ?"%I64d\n" : "%I64d ",ans.x[i]);
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120