- // Sudhir Krishnakumar - IT 2nd Year
- #include<iostream>
- #include<vector>
- #include<set>
- #include<map>
- using namespace std;
- #define FOR(i,a,b) for(int i=(a);i<(b);i++)
- #define SZ(x) ((int)(x).size())
- #define FORV(i,x) FOR(i,0,SZ(x))
- #define F first
- #define S second
- map<string,string> m,ans,rep;
- vector< pair<string,string> > v;
- set<string> found;
- int main()
- {
- string lhs,rhs;
- int n;
- cin >> n;
- FOR(i,0,n)
- {
- cin >> lhs >> rhs;
- v.push_back(make_pair(lhs,rhs));
- }
- FORV(i,v)
- {
- string lhs=v[i].F,rhs=v[i].S,temp1,temp2;
- int k;
- for(k=0;rhs[k]!='+';k++) temp1+=rhs[k];
- for(k++;k<SZ(rhs);k++) temp2+=rhs[k];
- for( map<string,string> :: iterator it=rep.begin() ; it!=rep.end() ; it++ )
- {
- if(temp1==it->F) temp1=rep[it->F];
- if(temp2==it->F) temp2=rep[it->F];
- }
- string str=temp1;
- str+="+";
- str+=temp2;
- rhs=v[i].S=str;
- if(m.find(rhs)==m.end())
- {
- m[rhs]=lhs;
- found.insert(lhs);
- }
- else
- {
- string r=m[rhs];
- int k;
- if(!(found.find(temp1)!=found.end() || found.find(temp2)!=found.end()))
- {
- bool f=0;
- FOR(x,0,i) if(v[x].S==rhs)
- {
- rep[v[i].F]=v[x].F;
- v[i].F=v[x].F;
- found.insert(v[x].F);
- f=1;
- break;
- }
- if(!f) found.insert(v[i].F);
- }
- }
- }
- FORV(i,v) ans[v[i].F]=v[i].S;
- for( map<string,string> :: iterator it=ans.begin() ; it!=ans.end() ; it++ )
- {
- cout << (it->F) << "=" << (it->S) << endl;
- }
- return 0;
- }
Untitled
Posted by Anonymous on Tue 14th Apr 2015 13:46
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.