fork download
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3. using namespace std;
  4. using namespace chrono;
  5.  
  6.  
  7. using ll = long long;
  8. using ull = unsigned long long;
  9. using ld = double;
  10. using vll = vector<ll>;
  11.  
  12. constexpr ll INF = 4e18;
  13. constexpr ld EPS = 1e-9;
  14. constexpr ll MOD = 1e9 + 7;
  15.  
  16. #define F first
  17. #define S second
  18. #define all(x) begin(x), end(x)
  19. #define allr(x) rbegin(x), rend(x)
  20. #define py cout<<"YES\n";
  21. #define pn cout<<"NO\n";
  22. #define forn(i, l, r) for (int i = (l); i <= (r); i++)
  23.  
  24. #define insert push_back
  25. #define pb push_back
  26. #define MP make_pair
  27. #define endl '\n'
  28.  
  29.  
  30. void AJNJ(){
  31. string s , t;
  32. cin >> s;
  33. t = s;
  34. ll n = s.size();
  35. sort(all(t));
  36. if(t == s){
  37. cout << "0" << endl;
  38. return;
  39. }
  40. if(s[0]==t[0] || s[n-1]==t[n-1]){
  41. cout<<"1"<<endl;
  42. }
  43. else if(s[0]==t[n-1] || s[n-1]==t[0]){
  44. cout<<"3"<<endl;
  45. }
  46. else{
  47. cout<<"2"<<endl;
  48. }
  49. }
  50.  
  51.  
  52. int main(){
  53. ios::sync_with_stdio(0);
  54. cin.tie(0);
  55. cout.tie(0);
  56. int T = 1;
  57. cin>>T;
  58. auto start1 = high_resolution_clock::now();
  59. while(T--){
  60. AJNJ();
  61. }
  62. auto stop1 = high_resolution_clock::now();
  63. auto duration = duration_cast<microseconds>(stop1 - start1);
  64. cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
  65.  
  66. return 0;
  67. }
Success #stdin #stdout #stderr 0s 5320KB
stdin
1
zyxpqa
stdout
3
stderr
Time: 0 ms