Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- typedef long long ll;
- typedef long double ld;
- using namespace std;
- set<string> findMax(set<string> gens, set<string>::iterator itt){
- set<string> res;
- res=gens;
- set<string>::iterator it = itt;
- string s = *it;
- //cout << s << "=" << ' ' << endl;
- bool b=false;
- if (gens.size()==1)
- return gens;
- if ((*itt)==(*(gens.rbegin()))){
- set<string> ss;
- ss = gens;
- string s1, s2;
- s1=*gens.begin();
- s2=*(++gens.begin());
- ss.erase(s1);
- ss.erase(s2);
- string t=s1+s2;
- //cout << t << "+" << endl;
- ss.insert(t);
- b=true;
- res = findMax(ss, ss.begin());
- }
- //cout << "++++++++++++++++++++" << endl;
- for (ll i=1; i<s.length(); i++){
- for (auto&gen:gens){
- //cout << gen << ' ' << s << "++"<< ((gen!=s)&&(gen.find(s.substr(i))==0)) << endl;
- if ((gen!=s)&&(gen.find(s.substr(i))==0)){
- b=true;
- set<string> ss;
- ss = gens;
- ss.erase(gen);
- ss.erase(s);
- string t=s+gen.substr(s.length()-i);
- ss.insert(t);
- //cout << t << '-' << endl;
- res = findMax(ss, ss.begin());
- }
- }
- if (b)
- break;
- }
- if (!b)
- res=findMax(gens, ++it);
- return res;
- }
- int main()
- {
- cin.sync_with_stdio(false);
- cout.sync_with_stdio(false);
- ll n, a;
- cin >> n;
- string s;
- set<string> gens;
- for (ll i=0; i<n; i++){
- cin >> s;
- gens.insert(s);
- }
- set<string> del;
- set<string>::iterator it = gens.begin();
- while (it != gens.end()){
- set<string>::iterator jt=gens.begin();
- while (jt != gens.end()){
- if ((jt!=it)&&((*it).find(*jt) != std::string::npos))
- del.insert(*jt);
- //gens.erase(*jt);
- jt++;
- }
- it++;
- }
- for (auto&d:del)
- gens.erase(d);
- /*for (auto&gen:gens)
- cout << gen << endl;*/
- set<string> res;
- res = findMax(gens, gens.begin());
- for (auto&gen:res)
- cout << gen << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement