Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1.     #include <bits/stdc++.h>
  2.      
  3.     using namespace std;
  4.      
  5.     // Debugging macros
  6.     #define TRACE(x)
  7.     #define WATCH(x) TRACE( cout << #x" = " << x << endl)
  8.     #define PRINT(x) TRACE(printf(x))
  9.     #define WATCHR(a, b) TRACE( for(auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
  10.     #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end()); } )
  11.     // End of Debugging macros
  12.      
  13.     // Macros for faster typing
  14.     #define all(v) (v).begin(), (v).end()
  15.     #define rall(v) (v).rbegin(), (v).rend()
  16.     #define sz(v) (int) (v).size()
  17.     #define rep(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
  18.     #define pb push_back
  19.      
  20.     using ll = long long;
  21.     using vi = vector<int>;
  22.     using pii = pair<int, int>;
  23.     using pll = pair<ll, ll>;
  24.     // End of macros for faster typing
  25.      
  26.     constexpr int inf = 0x3f3f3f3f;
  27.     constexpr ll MOD = 1000000007LL;
  28.     constexpr double tol = 1e-8;
  29.      
  30.     inline ll powmod( ll a, ll b, ll mod = MOD) {
  31.         ll res = 1; a %= mod; assert(b >= 0);
  32.         for(;b;b>>=1) {
  33.             if(b&1) res = (res * a) % mod;
  34.             a = (a * a) % mod;
  35.         }
  36.         return res;
  37.     }
  38.      
  39.     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  40.     constexpr int ms = 2e5 + 13;
  41.     vector< int > pref[ms], suff[ms];
  42.     int main() {
  43.         ios::sync_with_stdio(0);cin.tie(NULL);
  44.         // obs, vai sempre ser um prefixo ou um sufixo
  45.         string s, t;
  46.         cin >> s >> t;
  47.         int len_s = sz(s), len_t = sz(t);
  48.         vector< int > tam_pref(len_s);
  49.         vector< int > tam_suff(len_s);
  50.      
  51.         int cur_idx = 0;
  52.         for(int i = 0; i < len_s; ++i) {
  53.             if( cur_idx == len_t ) tam_pref[i] = len_t;
  54.             else {
  55.                 if(s[i] == t[cur_idx]) ++cur_idx;
  56.                 tam_pref[i] = cur_idx;
  57.             }
  58.         }
  59.      
  60.         cur_idx = len_t - 1;
  61.         for(int i = len_s - 1; i >= 0; --i) {
  62.             if( cur_idx == -1 ) tam_suff[i] = len_t;
  63.             else {
  64.                 if( s[i] == t[cur_idx]) --cur_idx;
  65.                 tam_suff[i] = len_t - cur_idx - 1;
  66.             }
  67.         }
  68.         int ans = 0;    
  69.         for(int i = 0; i < len_s; ++i)
  70.         {
  71.             pref[ tam_pref[i] ].push_back(i);
  72.             suff[ tam_suff[i] ].push_back(i);
  73.             if( tam_pref[i] == len_t ) ans = max( ans, len_s - 1 - i);
  74.             if( tam_suff[i] == len_t ) ans = max( ans, i );
  75.         }
  76.         WATCHC( tam_pref );
  77.         WATCHC( tam_suff );
  78.         // agora tem os casos interessantes que tem que quebrar no meio
  79.         for(int i = 0; i < len_s; ++i)
  80.         {
  81.             int good_pref = tam_pref[i];
  82.             if( good_pref == len_t ) continue;
  83.             int need = len_t - good_pref;
  84.             int L = suff[need].back();
  85.             if( L > i )
  86.             {
  87.                 int cut = L - i - 1;
  88.                 ans = max( cut, ans );
  89.             }
  90.         }
  91.         cout << ans << endl;
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement