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.

给你一个由加号、乘号、括号、一位数以及未知数x组成的多项式。例如,一个有效多项式可以是:(1+x)*(1+x*x+x+5)+1*x*x。

现在你需要写出与原式等价的最小多项式形式。

例如,(1+x)*(1+x)应该改写成x^2+2*x+1。

同理,(1+x)*(1+x*x+x+5)+1*x*x 应该改写成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.

输入的首行是一个整数T,表示测试样例的数量。1 <= T <= 50。

对于每个测试样例,都有一个长度不超过1000的多项式。

保证输入字符串均有效,并且所有数字都是一位数。

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).

输出每个多项式的最小形式。如果最小多项式有n项,则输出n个用空格分隔的整数。

你只要输出每项对应的系数(从高到低)。为防止输出过大,输出的结果模1000000007(1e9 + 7)。

Sample Input - 输入样例

Sample Output - 输出样例

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

3

1 2 1

1 3 7 6 0

2 0 0

【题解】

类似表达式求值的做法,表达式求值分为数字栈和符号栈,这里的区别就是把数字栈换成多项式栈。当然,由于储存方式的不同,写法也不唯一。

需要注意的是运算时候数据是否溢出。

【代码 C++】

下面贴出代码版本比较原始,不过个人认为可读性比较好,算法简单。当然留了优化的空间,有兴趣(强迫症)可以试试……

注意,因为下面代码中使用的gets()由于不安全,C11标准中删除了gets(),使用一个新的更安全的函数gets_s()替代。

 #include<cstdio>
#include<cstring>
__int64 polynomial[][], mi, pi;
char data[], mark[], *di;
bool j_mark(){
switch (*di){
case : return ;
case '(': return ;
case ')': return ;
case '+':
if (mark[mi] == '*' || mark[mi] == '+') return ;
else return ;
case '*':
if (mark[mi] == '*') return ;
return ;
default: return ;
}
}
bool j_count(){
switch (*di){
case : return mark[mi] ? : ;
case ')':
if (mark[mi] != '(') return ;
--mi;
return ;
case '+':
if (mark[mi] == '*' || mark[mi] == '+') return ;
mark[++mi] = '+';
return ;
case '*':
if (mark[mi] == '*') return ;
mark[++mi] = '*';
return ;
default: return ;
}
}
void count(){
__int64 temp[];
memset(temp, , sizeof(temp));
if (mark[mi] == '+'){
for (int i = ; i <= ; ++i)
temp[i] = (polynomial[pi][i] + polynomial[pi - ][i]) % ;
}
else{
int i, j;
for (i = ; i <= ; ++i){
if (!polynomial[pi][i]) continue;
for (j = ; j <= ; ++j){
if (!polynomial[pi - ][j]) continue;
temp[i + j] += (polynomial[pi][i] * polynomial[pi - ][j]) % ;
temp[i + j] %= ;
}
}
}
--pi; --mi;
memcpy(polynomial[pi], temp, sizeof(temp));
return;
}
void opt(){
int st = ;
while (st){
if (!polynomial[][st]) --st;
else break;
}
printf("%d", polynomial[][st]);
for (--st; ~st; --st) printf(" %d", polynomial[][st]);
puts("");
return;
}
int main(){
int T;
scanf("%d", &T); getchar();
while (T--){
pi = -; mark[] = mi = ;
for (gets(data), di = data; true; ++di){
if (*di < ''){//符号
if (j_mark()) mark[++mi] = *di;
else{
while (j_count()) count();
}
}
else{
++pi;
memset(polynomial[pi], , sizeof(polynomial[pi]));
if (*di > '') polynomial[pi][] = ;
else polynomial[pi][] = *di - '';
}
if (!*di) break;
}
opt();
}
return ;
}

FZU 2215

FZU 2215

FZU 2215 Simple Polynomial Problem(简单多项式问题)的更多相关文章

  1. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  3. FZU 2218 Simple String Problem(简单字符串问题)

    Description 题目描述 Recently, you have found your interest in string theory. Here is an interesting que ...

  4. FZU - 2218 Simple String Problem 状压dp

    FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...

  5. FZU2215 Simple Polynomial Problem(中缀表达求值)

    比赛时没做出这题太可惜了. 赛后才反应过来这就是个中缀表达式求值,数字栈存的不是数字而是多项式. 而且,中缀表达式求值很水的,几行就可以搞定. #include<cstdio> #incl ...

  6. 一起啃PRML - 1.1 Example: Polynomial Curve Fitting 多项式曲线拟合

    一起啃PRML - 1.1 Example: Polynomial Curve Fitting @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ 前言: ...

  7. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  8. hdu4976 A simple greedy problem. (贪心+DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...

  9. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. Spring之AOP面向切片

       一.理论基础: AOP(Aspectoriented programming)面向切片/服务的编程,在Spring中使用最多的是对事物的处理.而AOP这种思想在程序中很多地方可以使用的,比如说, ...

  2. ch2-4:遇到嵌套列表进行缩进打印

    1.增加一个参数来控制缩进打印:level '''这是一个模块,可以打印列表,其中可能包含嵌套列表''' def print_list(the_list,level): ""&qu ...

  3. Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

    docs.jboss.org文档示例代码:(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/) sta ...

  4. PHP笔记随笔

    1.CSS控制页面文字不能复制: body{-webkit-user-select:none;}   2.[php过滤汉字和非汉字] $sc="aaad....##--__i汉字过滤&quo ...

  5. oracle进制-10进制跟2进制互转

    CREATE OR REPLACE FUNCTION NUMBER_TO_BIT(V_NUM NUMBER) RETURN VARCHAR IS V_RTN );--注意返回列长度 V_N1 NUMB ...

  6. Apache2.2 + php-5.4.45-Win32-VC9-x86 配置

    首先要注意一个问题是:网上有很多教程比如: 在Apache配置文件中添加php模块.在apache2\conf\httpd.conf中: LoadModule模块添加行: LoadModule php ...

  7. /proc/sysrq-trigger的功能 介绍

    介绍/proc/sysrq-trigger的强大功能 让大家了解一下,在linux里,可以利用/proc/sysrq-trigger做些事情 # 立即重新启动计算机echo "b" ...

  8. 【转】Eclipse Console 加大显示的行数,禁止弹出

    转载地址:http://blog.csdn.net/leidengyan/article/details/5686691 Eclipse Console 加大显示的行数: 在 Preferences- ...

  9. js去除日期字符串时分秒

    var date = "2015-11-11 00:00:00"; var newDate=/\d{4}-\d{1,2}-\d{1,2}/g.exec(date) newDate= ...

  10. 数据库连接池(DBCP:为数据统一建立一个缓冲池,现在企业开发使用)

    数据库连接池:(里面放了许多连接数据的链接,负责分配,管理,释放数据库连接,可重复使用连接,而不新建  )为数据统一连接建立一个缓冲池,放好了一定数据库连接,使用时在缓冲池里面拿,用完之后再还给缓冲池 ...