Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         string str1, str2;
  8.         cin >> str1 >> str2;
  9.         if (str1 == str2)
  10.         {
  11.                 cout << str1;
  12.                 return 0;
  13.         }
  14.         int N,N2;
  15.         if (str1.length() > str2.length())
  16.         {
  17.                 N = str2.length();
  18.                 swap(str1, str2);
  19.         }
  20.         else N = str1.length();
  21.  
  22.         N2 = str2.length();
  23.  
  24.         int maxCount = 0, ind1 = 0, ind2 = 0, start = 0, finish = 0;
  25.        
  26.         while (ind1 != N - 1)
  27.         {
  28.                 if (maxCount == N - 1) break;
  29.  
  30.                 if (str1[ind1] == str2[ind2])
  31.                 {
  32.                         int tmpStart = ind1, tmp1 = ind1 + 1, tmp2 = ind2 + 1, count = 1, barr = ind1, tmpFinish = ind1;
  33.                        
  34.                         while (barr != N - 1)
  35.                         {
  36.                                
  37.                                 if (str1[tmp1] == str2[tmp2])
  38.                                 {
  39.                                         count++;
  40.                                         tmpFinish = tmp1;
  41.                                         tmp1++;
  42.                                         tmp2++;
  43.                                         barr++;
  44.  
  45.                                 }
  46.                                 else
  47.                                 {
  48.                                         if (count > maxCount)
  49.                                         {
  50.                                                 maxCount = count;
  51.                                                 start = tmpStart;
  52.                                                 finish = tmpFinish;
  53.                                         }
  54.                                         count = 0;
  55.                                         ++ind2;
  56.                                         break;
  57.                                 }
  58.                                 if (count > maxCount)
  59.                                 {
  60.                                         maxCount = count;
  61.                                         start = tmpStart;
  62.                                         finish = tmpFinish;
  63.                                 }
  64.                                 if (barr == N - 1) ++ind2;
  65.                         }
  66.                        
  67.                 }
  68.                 else ++ind2;
  69.                
  70.                 if (ind2 == N2)
  71.                 {
  72.                         ind2 = 0;
  73.                         ++ind1;
  74.                 }
  75.                
  76.         }
  77.         /*if (maxCount == 1) cout << str1[start];
  78.         else {
  79.                 do
  80.                 {
  81.                         cout << str1[start];
  82.                         start++;
  83.                         maxCount--;
  84.                 } while (start <= finish);
  85.         }*/
  86.  
  87.         if (maxCount != 1)
  88.         {
  89.                 do
  90.                 {
  91.                         cout << str1[start];
  92.                         start++;
  93.                 } while (start <= finish);
  94.         }
  95.         else cout << str1[start];
  96.         return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement