Advertisement
silentkiler029

SlidingWindow_BinarySearch_ProbSoln_Shanto

Nov 22nd, 2020 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. /*    BISMILLAHIR-RAHMANIR-RAHIM
  2.  ____________________________________
  3. |                                    |
  4. |      SHANTO_SUST_SWE-19__029       |
  5. |      shanto-swe029.github.io       |
  6. |____________________________________|
  7. */
  8.  
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11.  
  12. bool isCorrect( string s, int n, int** cumSum )
  13. {
  14.     int len = s.size();
  15.     for( int i = n; i <= s.size(); i++ ) {
  16.         int x = ( cumSum[0][i] - cumSum[0][i - n] ) * ( cumSum[1][i] - cumSum[1][i - n] ) * ( cumSum[2][i] - cumSum[2][i - n] );
  17.         if ( x > 0 ) {
  18.             return true;
  19.         }
  20.     }
  21.     return false;
  22. }
  23.  
  24. int BinarySearch_SlidingWindow( string s, int** cumSum )
  25. {
  26.     int n = s.size();
  27.     int BEG = 1, END = n, MID;
  28.     int length = n + 2;
  29.  
  30.     while( BEG <= END ) {
  31.         MID = ( int ) ( BEG + END ) / 2;
  32.         bool b = isCorrect( s, MID, cumSum );
  33.         if( !b ) {
  34.             BEG = MID + 1;
  35.         }
  36.         else {
  37.             END = MID - 1;
  38.             if( MID < length ) {
  39.                 length = MID;
  40.             }
  41.         }
  42.     }
  43.    
  44.     return length;
  45. }
  46.  
  47. int main()
  48. {
  49.     #ifndef ONLINE_JUDGE
  50.     freopen("input.txt", "r", stdin);
  51.     freopen("output.txt", "w", stdout);
  52.     #endif
  53.  
  54.     string s;
  55.     std::cin >> s;
  56.     int **cumSum;
  57.     cumSum = (int **) malloc( sizeof(int *) * 3 );
  58.     for( int i = 0; i < 3; i++ ) {
  59.         cumSum[i] = (int *) malloc( sizeof(int *) * ( s.size() + 1 ) );
  60.     }
  61.  
  62.     cumSum[0][0] = 0;
  63.     cumSum[1][0] = 0;
  64.     cumSum[2][0] = 0;
  65.     int s1 = 0, s2 = 0, s3 = 0;
  66.     for( int i = 0; i < s.size(); i++ ) {
  67.         if( s[i] == '1' ) {
  68.             s1++;
  69.             cumSum[0][i+1] = s1;
  70.             cumSum[1][i+1] = s2;
  71.             cumSum[2][i+1] = s3;
  72.         }
  73.         else if( s[i] == '2' ) {
  74.             s2++;
  75.             cumSum[0][i+1] = s1;
  76.             cumSum[1][i+1] = s2;
  77.             cumSum[2][i+1] = s3;
  78.         }
  79.         else {
  80.             s3++;
  81.             cumSum[0][i+1] = s1;
  82.             cumSum[1][i+1] = s2;
  83.             cumSum[2][i+1] = s3;
  84.         }
  85.     }
  86.  
  87.     int ans = BinarySearch_SlidingWindow( s, cumSum );
  88.  
  89.     if( ans > s.size() ) std::cout << "0 ->> no substring found" << endl;
  90.     else std::cout << "minimum legth of the substring : " << ans << endl;
  91. }
  92.  
  93. //ALHAMDULILLAH
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement