Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- main()
- {
- //for t test cases
- int t;cin>>t;
- for(int i=0;i<t;i++)
- {
- long long int n,k;
- cin>>n>>k;
- //string A of the problem statement
- string s;cin>>s;
- //maximum possible string we get by performing the given operations
- string maxpossiblestring="";
- long long int c=0,ans=0;
- //vector(dynamic array) to store the no of operations we perform to get the
- //maximum possible string for the x'th time
- vector<long long int>operations;
- for(int i=0;i<n;i++)
- {
- c=0;
- if(i==0)
- maxpossiblestring=s;
- else
- {
- char cc=s[0];
- s.erase(0,1);
- s.push_back(cc);//appending the 1st char at the end of the string
- }
- //if curret string after performing the operation is greater than the maximum
- //string we got so far, erase all the operations and update the maximum
- //possible string
- if(maxpossiblestring<s)
- {
- operations.clear();
- maxpossiblestring=s;
- c=1;
- operations.push_back(i);
- }
- //store the operations for which we get the maximum possible string
- if(maxpossiblestring==s)
- {
- if(c==0)
- operations.push_back(i);
- }
- }
- if(k<=operations.size()){
- ans=operations[k-1];
- }
- else
- {
- // unitary method to get the answer.
- // if k is a multiple of no of occurences of maximum possible string in a cycle,
- // then ans = no of k/occurence_count -1 + last element of the operations array
- if(k%operations.size()==0)
- {
- ans=((k/operations.size() - 1)*n);
- ans+=operations[operations.size()-1];
- }
- else
- {
- // unitary method to get the answer.
- // if k is not a multiple of no of occurences of maximum possible string in a cycle,
- // then ans = no of k/occurence_count + operations to get (k%occurence) occurence of
- // maximum possible string
- ans=((k/operations.size())*n);
- ans+=operations[k%operations.size()-1];
- }
- }
- cout<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement