fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. void solve()
  7. {
  8.  
  9. ll n;
  10. cin>>n;
  11. string a , b;
  12. cin>>a>>b;
  13.  
  14. vector<ll>freq(30);
  15. vector<bool>flags(n+5,0);
  16.  
  17. for (auto e :a) freq[e-'a']++;
  18.  
  19. if (a<b){cout<<"Yes"; return; }
  20.  
  21. for (int i = 0 ; i < n ; i++)
  22. {
  23.  
  24. for (int j = 0 ; j < b[i] - 'a' ; j++)
  25. {
  26. if (freq[j]) {cout<<"Yes"; return; }
  27. }
  28.  
  29. if (a[i] > b[i] && !freq[b[i]-'a']) {cout <<"No"; return;}
  30. if (a[i] == b[i]){freq[a[i]-'a']--; continue;}
  31.  
  32.  
  33. }
  34.  
  35. cout << "No"; return;
  36. }
  37.  
  38. int main()
  39. {
  40. ios::sync_with_stdio(0);
  41. cin.tie(0);
  42. cout.tie(0);
  43.  
  44.  
  45. int t = 1;
  46. cin >> t;
  47.  
  48. while (t--)
  49. {
  50. solve();
  51. cout <<'\n';
  52. }
  53.  
  54. return 0;
  55. }
  56. /*
  57. int j = n-1 , curridx = i , fg = 0 , lst;
  58. while (j>=i)
  59. {
  60. if (a[j] == b[j]) flags[j] = flags[j+1];
  61. else if (a[j]>b[j]) flags[j] = 0;
  62. else flags[j] = 1;
  63.  
  64. if (a[j] == b[i])
  65. {
  66. curridx = i;
  67. fg = 1;
  68. lst = j; j--;
  69. continue;
  70. }
  71.  
  72. if (fg)
  73. {
  74. while (j>=i && b[i] != a[j])
  75. {
  76. if (a[j] == b[j]) flags[j] = flags[j+1];
  77. else if (a[j]>b[j]) flags[j] = 0;
  78. else flags[j] = 1;
  79.  
  80. if (a[j] < b[curridx]) {cout<<"Yes"; return; }
  81. else if (a[j] > b[curridx]) {fg = 0; break;}
  82. curridx++; j--;
  83. }
  84. if (fg == 1 && flags[lst+1]) {cout<<"Yes"; return; }
  85. }
  86.  
  87. j--;
  88. }
  89. break;
  90.  */
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
No