arujbansal

CF 46 Q1

Sep 5th, 2021
1,826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void dbg_out() { cerr << endl; }
  6. template<typename Head, typename... Tail>
  7. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  8. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  9.  
  10. #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
  11. #define rng_seed(x) mt19937 rng(x)
  12. #define all(x) (x).begin(), (x).end()
  13. #define sz(x) (int) (x).size()
  14. // #define int long long
  15.  
  16. const int MXN = 1e5 + 5, INF = 1e9 + 5;
  17.  
  18. void solve() {
  19.     string S, T;
  20.     cin >> S >> T;
  21.  
  22.     int N = sz(S), M = sz(T);
  23.     if (N != M) {
  24.         cout << "NO";
  25.         return;
  26.     }
  27.  
  28.     if (S == T) {
  29.         cout << "YES";
  30.         return;
  31.     }
  32.  
  33.     if (S.front() != T.front() && S.back() != T.back()) {
  34.         cout << "NO";
  35.         return;
  36.     }
  37.  
  38.     // if (S.front() == T.front()) {
  39.     //     reverse(all(S));
  40.     //     reverse(all(T));
  41.     // }
  42.  
  43.     int mn = INF, mx = -INF;
  44.     int last = -1;
  45.     for (int i = 0; i < N - 1; i++) {
  46.         if (S[i] == T[i]) continue;
  47.  
  48.         if (S[i] != T[i] && last != -1 && last < i - 1) {
  49.             cout << "NO";
  50.             return;
  51.         }
  52.  
  53.         if (S[i] != T[i]) last = i;
  54.  
  55.         if (S[i] > T[i]) {
  56.             S[i] = (char) ('a' - 1);
  57.         }
  58.  
  59.         // dbg(i, moves);
  60.  
  61.  
  62.         mn = min(mn, (S[i] - 'a') - (T[i] - 'a'));
  63.         mx = max(mn, (S[i] - 'a') - (T[i] - 'a'));
  64.         // dbg(i, mn, mx);
  65.     }
  66.  
  67.     cout << (mx == mn ? "YES" : "NO");
  68. }
  69.  
  70. signed main() {
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(nullptr);
  73.  
  74.     int TC = 1;
  75.     // cin >> TC;
  76.     while (TC--) solve();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment