Advertisement
Alexandre_lsv

Untitled

Mar 20th, 2016
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. typedef long double ld;
  4. using namespace std;
  5.  
  6.  
  7. set<string> findMax(set<string> gens, set<string>::iterator itt){
  8.     set<string> res;
  9.     res=gens;
  10.     set<string>::iterator it = itt;
  11.     string s = *it;
  12.     //cout << s << "=" << ' ' << endl;
  13.     bool b=false;
  14.     if (gens.size()==1)
  15.         return gens;
  16.     if ((*itt)==(*(gens.rbegin()))){
  17.         set<string> ss;
  18.         ss = gens;
  19.         string s1, s2;
  20.         s1=*gens.begin();
  21.         s2=*(++gens.begin());
  22.         ss.erase(s1);
  23.         ss.erase(s2);
  24.         string t=s1+s2;
  25.         //cout << t << "+" << endl;
  26.         ss.insert(t);
  27.         b=true;
  28.         res = findMax(ss, ss.begin()); 
  29.     }
  30.     //cout << "++++++++++++++++++++" << endl;
  31.     for (ll i=1; i<s.length(); i++){
  32.         for (auto&gen:gens){
  33.             //cout << gen << ' ' << s << "++"<< ((gen!=s)&&(gen.find(s.substr(i))==0)) << endl;
  34.             if ((gen!=s)&&(gen.find(s.substr(i))==0)){
  35.                 b=true;
  36.                 set<string> ss;
  37.                 ss = gens;
  38.                 ss.erase(gen);
  39.                 ss.erase(s);
  40.                 string t=s+gen.substr(s.length()-i);
  41.                 ss.insert(t);
  42.                 //cout << t << '-' << endl;
  43.                 res = findMax(ss, ss.begin());
  44.             }
  45.         }
  46.         if (b)
  47.             break;
  48.     }
  49.     if (!b)
  50.         res=findMax(gens, ++it);
  51.     return res;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     cin.sync_with_stdio(false);
  58.     cout.sync_with_stdio(false);
  59.    ll n, a;
  60.    cin >> n;
  61.    string s;
  62.    set<string> gens;
  63.     for (ll i=0; i<n; i++){
  64.         cin >> s;
  65.         gens.insert(s);
  66.    }
  67.    set<string> del;
  68.    set<string>::iterator it = gens.begin();
  69.    while (it != gens.end()){
  70.         set<string>::iterator jt=gens.begin();
  71.          while (jt != gens.end()){
  72.             if ((jt!=it)&&((*it).find(*jt) != std::string::npos))
  73.                 del.insert(*jt);
  74.                 //gens.erase(*jt);             
  75.         jt++;
  76.         }
  77.         it++;
  78.     }
  79.  
  80.     for (auto&d:del)
  81.         gens.erase(d);
  82.     /*for (auto&gen:gens)
  83.         cout << gen << endl;*/
  84.  
  85.  
  86.     set<string> res;
  87.     res = findMax(gens, gens.begin());
  88.  
  89.     for (auto&gen:res)
  90.         cout << gen << endl;
  91.  
  92.    return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement