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

思路 : 在已经有了字符串求值的算法以后,就很容易理解这个题了我们可以将字符串里的每个非运算符的字符都当成一个多项式,那么这样的话,就能把这个问题转化为字符串求值问题。

例如  (1+x)*(1+x) 就变成了   (1+2)*(3+4) 其中每个数字代表一个多项式,也就是多项式数组的下标。

#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;
}

购物商城项目采用PHP+mysql有以及html+css jq以及layer.js datatables bootstorap等插件等开发,采用了MVC模式,建立一个完善的电商系统,通过不同用户的不同需求,进行相应的调配和处理,提高对购买用户进行配置….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值