Advertisement
fireLUFFY

HackerEarth_CodeMonk_CyclicShift

Jan 14th, 2022
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main()
  5. {
  6.     //for t test cases
  7.     int t;cin>>t;
  8.     for(int i=0;i<t;i++)
  9.     {
  10.         long long int n,k;
  11.         cin>>n>>k;
  12.         //string A of the problem statement
  13.         string s;cin>>s;
  14.  
  15.         //maximum possible string we get by performing the given operations
  16.         string maxpossiblestring="";
  17.  
  18.         long long int c=0,ans=0;
  19.        
  20.         //vector(dynamic array) to store the no of operations we perform to get the
  21.         //maximum possible string for the x'th time
  22.         vector<long long int>operations;
  23.  
  24.         for(int i=0;i<n;i++)
  25.         {
  26.             c=0;
  27.             if(i==0)
  28.                 maxpossiblestring=s;
  29.             else
  30.             {
  31.                 char cc=s[0];
  32.                 s.erase(0,1);
  33.                 s.push_back(cc);//appending the 1st char at the end of the string
  34.             }
  35.  
  36.             //if curret string after performing the operation is greater than the maximum
  37.             //string we got so far, erase all the operations and update the maximum
  38.             //possible string
  39.             if(maxpossiblestring<s)
  40.             {
  41.                 operations.clear();
  42.                 maxpossiblestring=s;
  43.                 c=1;
  44.                 operations.push_back(i);
  45.             }
  46.            
  47.             //store the operations for which we get the maximum possible string
  48.             if(maxpossiblestring==s)
  49.             {
  50.                 if(c==0)
  51.                     operations.push_back(i);
  52.             }
  53.         }
  54.  
  55.         if(k<=operations.size()){
  56.             ans=operations[k-1];
  57.         }
  58.         else
  59.         {
  60.             // unitary method to get the answer.
  61.             // if k is a multiple of no of occurences of maximum possible string in a cycle,
  62.             // then ans = no of k/occurence_count -1 + last element of the operations array
  63.             if(k%operations.size()==0)
  64.             {
  65.                 ans=((k/operations.size() - 1)*n);
  66.                 ans+=operations[operations.size()-1];
  67.             }
  68.             else
  69.             {
  70.                 // unitary method to get the answer.
  71.                 // if k is not a multiple of no of occurences of maximum possible string in a cycle,
  72.                 // then ans = no of k/occurence_count + operations to get (k%occurence) occurence of
  73.                 // maximum possible string
  74.                 ans=((k/operations.size())*n);
  75.                 ans+=operations[k%operations.size()-1];
  76.             }
  77.         }
  78.         cout<<ans<<endl;
  79.     }
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement