A DP Problem Asia - Tehran 2003

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



Source: Asia - Tehran 2003




my code:

#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;
}
['Asia/Aden', 'Asia/Almaty', 'Asia/Amman', 'Asia/Anadyr', 'Asia/Aqtau', 'Asia/Aqtobe', 'Asia/Ashgabat', 'Asia/Ashkhabad', 'Asia/Atyrau', 'Asia/Baghdad', 'Asia/Bahrain', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Barnaul', 'Asia/Beirut', 'Asia/Bishkek', 'Asia/Brunei', 'Asia/Calcutta', 'Asia/Chita', 'Asia/Choibalsan', 'Asia/Chongqing', 'Asia/Chungking', 'Asia/Colombo', 'Asia/Dacca', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dili', 'Asia/Dubai', 'Asia/Dushanbe', 'Asia/Famagusta', 'Asia/Gaza', 'Asia/Harbin', 'Asia/Hebron', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong', 'Asia/Hovd', 'Asia/Irkutsk', 'Asia/Istanbul', 'Asia/Jakarta', 'Asia/Jayapura', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Kamchatka', 'Asia/Karachi', 'Asia/Kashgar', 'Asia/Kathmandu', 'Asia/Katmandu', 'Asia/Khandyga', 'Asia/Kolkata', 'Asia/Krasnoyarsk', 'Asia/Kuala_Lumpur', 'Asia/Kuching', 'Asia/Kuwait', 'Asia/Macao', 'Asia/Macau', 'Asia/Magadan', 'Asia/Makassar', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia', 'Asia/Novokuznetsk', 'Asia/Novosibirsk', 'Asia/Omsk', 'Asia/Oral', 'Asia/Phnom_Penh', 'Asia/Pontianak', 'Asia/Pyongyang', 'Asia/Qatar', 'Asia/Qostanay', 'Asia/Qyzylorda', 'Asia/Rangoon', 'Asia/Riyadh', 'Asia/Saigon', 'Asia/Sakhalin', 'Asia/Samarkand', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Srednekolymsk', 'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tbilisi', 'Asia/Tehran', 'Asia/Tel_Aviv', 'Asia/Thimbu', 'Asia/Thimphu', 'Asia/Tokyo', 'Asia/Tomsk', 'Asia/Ujung_Pandang', 'Asia/Ulaanbaatar', 'Asia/Ulan_Bator', 'Asia/Urumqi', 'Asia/Ust-Nera', 'Asia/Vientiane', 'Asia/Vladivostok', 'Asia/Yakutsk', 'Asia/Yangon', 'Asia/Yekaterinburg', 'Asia/Yerevan']
最新发布
02-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值