Advertisement
Alexandre_lsv

Untitled

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