Advertisement
vercas

My code - subject 2

Mar 4th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. ifstream in("subsecvente.in");
  11. ofstream out("subsecvente.out");
  12.  
  13. int N;
  14. vector<string> Elemente;
  15.  
  16. bool lenSort(string a, string b)
  17. {
  18.     return a.length() < b.length();
  19. }
  20.  
  21. int main()
  22. {
  23.     in >> N;
  24.  
  25.     for (int i = 0; i < N; i++)
  26.     {
  27.         string temp;
  28.         in >> temp;
  29.         Elemente.push_back(temp);
  30.     }
  31.  
  32.     sort(Elemente.begin(), Elemente.end(), lenSort);
  33.  
  34.     string prim = Elemente.front();
  35.     int prim_len = (int)prim.length();
  36.  
  37.     int max = 1;
  38.     bool ok = true;
  39.     string sub;
  40.  
  41.     for (int start = 0; start < prim_len - 2; start++)
  42.         for (int end = 2; end <= min(prim_len - start, 60) + 2; end++)
  43.         {
  44.             sub = prim.substr(start, end);
  45.             ok = true;
  46.  
  47.             for (int n = 1; n < N; n++)
  48.             {
  49.                 //cout << start << "\t" << end << "\t" << Elemente[n] << "\t" << sub << "\t" << (Elemente[n].find(sub) == string::npos) << endl;
  50.                 if (Elemente[n].find(sub) == string::npos)
  51.                     ok = false;
  52.             }
  53.  
  54.             if (ok)
  55.             {
  56.                 if (sub.length() > max)
  57.                     max = sub.length();
  58.             }
  59.             else break;
  60.         }
  61.  
  62.     out << max;
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement