A DP Problem c++ ACM

来源:互联网 发布:淘宝网野生榛蘑 编辑:程序博客网 时间:2024/04/30 03:50
 
Program 3 A DP Problem (TOJ_1283)
 
In this problem, you are to solve a very easy linear equation with only one variable x with no parentheses! An example of such equations is like the following:
2x - 4 + 5x + 300 = 98x
An expression in its general form, will contain a '=' character with two expressions on its sides. Each expression is made up of one or more terms combined by '+' or '-' operators. No unary plus or minus operators are allowed in the expressions. Each term is either a single integer, or an integer followed by the lower-case character x or the single character x which is equivalent to 1x.
You are to write a program to find the value of x that satisfies the equation. Note that it is possible for the equation to have no solution or have infinitely many. Your program must detect these cases too.
Input
The first number in the input line, t (1 ≤ t ≤ 10) is the number of test cases, followed by t lines of length at most 255 each containing an equation. There is no blank character in the equations and the variable is always represented by the lower-case character x. The coefficients are integers in the range (0 ... 1000) inclusive.
Output
The output contains one line per test case containing the solution of the equation. If s is the solution to the equation, the output line should contain |_s_| (the floor of s, i.e., the largest integer number less than or equal to s). The output should be "IMPOSSIBLE" or "IDENTITY" if the equation has no solution or has infinite solutions, respectively. Note that the output is case-sensitive.
Sample Input
2
2x-4+5x+300=98x
x+2=2+x
Sample Output
3
IDENTITY
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
using namespace std;
const double LIMIT=0.000001;
bool is_dgt(char ch){
    if(ch>='0'&&ch<='9')return true;
    else return false;
}
  
int dgt_x(string s,bool bl[],int dex){
    string str="";
    int i;
    for(i=dex-1;is_dgt(s[i])&&i>=0;--i) str+=s[i];
   
    reverse(str.begin(),str.end());
    istringstream sin(str);
    int x,tem=0;
    if(i<0){
       if(str=="")tem=1;
       else {sin>>x;tem=x;}
       for(int j=dex;j>=0;--j){bl[j]=0;}
    }
    else{
        if(s[i]=='-'){
            if(str=="") tem=-1;
            else{sin>>x;tem=x*(-1);}
        }   
        else{
            if(str=="") tem=1;
            else{sin>>x;tem=x;}
        } 
        for(int j=dex;j>=i&&i>=0;--j){bl[j]=0;}
    }
    return tem;
}
int dgt_dgt(string s,bool bl[],int bgn,int start){
    int i=bgn;
    string str="";
    int key=1;
    if(i==0||start){
        if(start) i=start;
        if(is_dgt(s[i])&&bl[i]){
            while(is_dgt(s[i])&&bl[i]){
                str+=s[i];
                ++i;
            }
        }
        else if(s[i]=='-'){
            key=-1;
            while(is_dgt(s[i+1])&&bl[i+1]){
                str+=s[i+1];
                ++i;                   
            }   
        }           
    }
    else{
        if(s[i]=='+'){
           while(is_dgt(s[i+1])&&bl[i+1]){
                str+=s[i+1];
                ++i;
           }    
        }
        else if(s[i]=='-'){
            key=-1;
            while(is_dgt(s[i+1])&&bl[i+1]){
                str+=s[i+1];
                ++i;                   
            }   
        }      
    }
    int x;
    istringstream sin(str);
    sin>>x;
    if(key==1)return x;
    else return x*(-1);     
}       
   
int main(){
    string str;
    getline(cin,str);
    int n=atoi(str.c_str());
    char ch,temp;
    bool bl[260];
    for(int i=0;i<n;++i){
       
        getline(cin,str);
        int len=str.length();
       
        memset(bl,1,260);
       
        int eql,l_x=0,l_dgt=0,r_x=0,r_dgt=0,l_bgn,r_end;
        for(eql=0;str[eql]!='=';eql++){;}
       
        for(int i=0;i<eql;++i) if(str[i]=='x') l_x+=dgt_x(str,bl,i);
       
        for(int i=0;i<eql;++i){
            if(bl[i]){
               if(i==0) l_dgt+=dgt_dgt(str,bl,i,0);
               else if(is_dgt(str[i])==0) l_dgt+=dgt_dgt(str,bl,i,0); 
            }   
        } 
       
        for(int i=eql+1;i<len;++i) if(str[i]=='x') r_x+=dgt_x(str,bl,i);
       
        for(int i=eql+1;i<len;++i){
            if(bl[i]){
               if(i==eql+1) r_dgt+=dgt_dgt(str,bl,i,i);
               else if(is_dgt(str[i])==0) r_dgt+=dgt_dgt(str,bl,i,0);
            }
        }   
        double ll_x=l_x-r_x,ans;
        double rr_dgt=r_dgt-l_dgt;   
        if(ll_x==0){
            if(rr_dgt==0) cout<<"IDENTITY";
            else cout<<"IMPOSSIBLE";
        }
        else{
          ans=rr_dgt/ll_x;
          if(fabs(ans)<LIMIT) cout<<0;
          else cout<<floor(ans); 
         
        } 
        cout<<endl;
    }
   
    return 0;