YZOI Easy Round 2 _ simplification (simplify. c/cpp/pas), yzoisimplify. c

Source: Internet
Author: User

YZOI Easy Round 2 _ simplification (simplify. c/cpp/pas), yzoisimplify. c

Description

If a polynomial is given, the simplified result is output.

Input

A string contains only the polynomials of the letter x, excluding parentheses and fractions, and no extra spaces.

Output

A string, a simplified polynomial, that outputs items in ascending order of number of times.

Input Sample

X ^ 3 + 3 * x ^ 4-2 * x ^ 3 + _x

Output Sample

3 * x ^ 4-x ^ 3-x + 1

Hint

The coefficient of each item is <10, the number of times is <6, and the number of items is <20. The length of a string cannot exceed 100.

The annoying simulation code is as follows:

#include<iostream>#include<cstdio>using namespace std;const int maxn=1000+10;string s;int a[maxn],lc=1,len;bool first=true;int main(){//freopen("simplify.in","r",stdin);//freopen("simplify.out","w",stdout);cin>>s;len=s.size();if(len==1){cout<<s[0];return 0;}for(int i=0;i<len;i++){if(s[i]=='x'){if(i==3){if(s[0]=='-'){if(s[4]=='^'){a[s[5]-'0']-=s[1]-'0';}else{a[1]-=s[1]-'0';}continue;}}if(s[0]>='0'&&s[0]<='9'&&i==2){if(s[3]=='^'){a[s[4]-'0']+=s[0]-'0';}else{a[1]+=s[0]-'0';}continue;}//if(i==0)//{//a[1]++;//continue;//}if(s[i+1]=='^'){if(i==0){a[s[i+2]-'0']++;continue;}if(s[i-1]=='*'){if(s[i-3]=='+')a[s[i+2]-'0']+=s[i-2]-'0';else if(s[i-3]=='-')a[s[i+2]-'0']-=(s[i-2]-'0');}else{if(i==0){a[s[i+2]-'0']++;continue;}if(s[i-1]=='+'||s[i-1]=='-'){if(s[i-1]=='-')a[s[i+2]-'0']--;elsea[s[i+2]-'0']++;}else{a[s[i+2]-'0']--;}}}else{if(s[i-1]=='+'){a[1]++;}else{a[1]--;}}}else{if(s[i]>='0'&&s[i]<='9'){if(s[i-1]=='^'||s[i+1]=='*')continue;a[0]+=s[i]-'0';}}}for(int i=0;i<len;i++){if(s[i]=='^')lc=max(lc,s[i+1]-'0');}for(int i=lc;i>=0;i--){if(a[i]==0)continue;if(i==0){if(a[i]>0){if(first){cout<<a[i];first=false;continue;}cout<<'+'<<a[i];}else{cout<<'-'<<a[i];}continue;}if(i==1){if(a[i]>0){if(a[i]==1){if(first){cout<<"x";first=false;continue;}cout<<"+x";}else{if(first){cout<<a[i]<<"*x";first=false;continue;}cout<<"+"<<a[i]<<"*x";}}else{if(a[i]==-1){cout<<"-x";}else{cout<<a[i]<<"*x";}}continue;}if(a[i]>0){if(a[i]==1){if(first){cout<<"x^"<<i;first=false;continue;}else{cout<<"+x^"<<i;}}else{if(first){cout<<a[i]<<"*x^"<<i;first=false;continue;}else{cout<<"+"<<a[i]<<"*x^"<<i;cout<<9999999<<endl;}}}else{if(a[i]==-1){cout<<"-x^"<<i;}else{cout<<a[i]<<"*x^"<<i;}}}//cout<<endl;//for(int i=8;i>=0;i--)//cout<<a[i]<<' ';//cout<<endl;//cout<<a[3]<<endl;return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.