Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- typedef vector<vector<int> > VV;
- typedef vector<int> V;
- void outv (VV & v)
- {
- for(int i=0;i<v.size();i++)
- {
- if(i!=0)
- {
- cout<<"||";
- }
- for(int j=0;j<v[i].size();j++)
- {
- if(j!=0)
- {
- cout<<"&";
- }
- if(v[i][j]<0)
- {
- cout<<"!";
- }
- cout<<"x"<<abs(v[i][j]);
- }
- }
- }
- int mystoi(string & s)
- {
- int a=0;
- int d=1;
- for(int i=s.length()-1; i>=0; i--)
- {
- a+=((int)s[i]-48)*d;
- d*=10;
- }
- //cout«"a="«a«endl;
- return a;
- }
- V pars(string & s)
- {
- V v1;
- int h = 1;
- string s1 = "";
- for(int i=0; i<s.size(); i++)
- {
- //cout«"i="«i«" s[i]="«s[i]«endl;
- if(s[i] == '!')
- {
- h = -1;
- }
- else if(s[i] >= '0' && s[i] <= '9')
- {
- //cout«"s[i]="«s[i]«endl;
- s1 += s[i];
- }
- else if(s[i] == '&')
- {
- //cout«"s1="«s1«endl;
- v1.push_back(mystoi(s1)*h);
- s1 = "";
- h = 1;
- }
- }
- v1.push_back(mystoi(s1)*h);
- return v1;
- }
- bool cmp_rule_1(V & v1,V & v2)
- {
- if(v1.size() != v2.size())
- {
- return false;
- }
- int g = 0;
- for(int i=0;i<v1.size();i++)
- {
- int h = 0;
- for(int j=0;j<v2.size();j++)
- {
- if(v1[i] == v2[j])
- {
- h = 1;break;
- }
- else
- {
- if(abs(v1[i]) == abs(v2[j]))
- {
- h = 2;break;
- }
- }
- }
- if(h == 0)
- {
- return false;
- }
- if(h == 2)
- {
- g++;
- }
- }
- if(g == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool cmp_rule_2(V & v1, V & v2)
- {
- if(v1.size() == v2.size())
- {
- return false;
- }
- for(int i=0; i<v1.size(); i++)
- {
- if(find(v2.begin(), v2.end(), v1[i]) == v2.end())
- {
- return false;
- }
- }
- return true;
- }
- V gluing(V & v1 , V & v2)
- {
- V v;
- for(int i=0; i<v1.size(); i++)
- {
- if(find(v2.begin(), v2.end(), v1[i]) != v2.end())
- {
- v.push_back(v1[i]);
- }
- }
- return v;
- }
- void reduction (VV & v)
- {
- for(int i=0;i<v.size();i++)
- {
- for(int j=i+1;j<v.size();j++)
- {
- int g=1;
- for(int k=0;k<v[i].size();k++)
- {
- int h=0;
- for(int l=0;l<v[j].size();l++)
- {
- if(v[i][k] == v[j][l])
- {
- h=1;
- break;
- }
- }
- if(h==0)
- {
- g=0;
- break;
- }
- }
- if(g==1)
- {
- // outv(v);
- // cout<<"i="<<i<<" j="<<j<<" reduction"<<'\n';
- v.erase(v.begin()+j);
- j--;
- }
- }
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- VV v;
- char c;
- string s = "";
- while (cin>>c)
- {
- if(c == '|')
- {
- //cout«"s="«s«endl;
- v.push_back(pars(s));
- s = "";
- cin>>c;
- }
- else
- {
- s += c;
- }
- }
- v.push_back(pars(s));
- //==========================input-OK!===========================//
- int h = 1, f=1;
- while(h != 0)
- {
- VV v1;
- h = 0;
- for(int i=0; i<v.size(); i++)
- {
- int l=0;
- for(int j=i+1; j<v.size(); j++)
- {
- if(cmp_rule_1(v[i],v[j]))
- {
- // vector<int>v2=gluing(v[i],v[j]);;
- // cout<<"i="<<i<<" j="<<j<<" gluing=";
- // for(int k =0;k<v2.size();k++)
- // {
- // cout<<v2[k]<<" ";
- // }
- // cout<<'\n';
- v1.push_back(gluing(v[i],v[j]));
- h++;
- }
- }
- if(l == 0 && f != 1)
- {
- v1.push_back(v[i]);
- }
- }
- if(h != 0)
- {
- v=v1;
- }
- for(int i=0; i<v.size(); i++)
- {
- for(int j=i+1; j<v.size(); j++)
- {
- if(cmp_rule_2(v[i],v[j]))
- {
- //outv(v);
- //cout<<"i="<<i<<" j="<<j<<" absorption";
- //cout<<'\n';
- v.erase(v.begin()+j);
- j--;
- h++;
- }
- else if(cmp_rule_2(v[j],v[i]))
- {
- v.erase(v.begin()+i);
- h++;
- i--;
- break;
- }
- }
- }
- reduction(v);
- f++;
- }
- outv(v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement