1316B - String Modification - CodeForces Solution


brute force constructive algorithms implementation sortings strings *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n = int(input())
    s = input()
    best = min(s)



    indexes = []
    for i, item in enumerate(s):
        if item == best:
            indexes.append(i)
    


    best_string = 'z'*10
    k = None
    for item in indexes:
                        
            
            
            
            
        
        
        
        string = s[item:]
        if (n-item)%2 == 0:string+=s[:item]
        else:string += s[:item][::-1]
        
        
        
        
        
        if "".join(string) < best_string:
            best_string = "".join(string)
            k = item
    
    
    print(best_string);print(k+1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
ll mod = 1e9+7;
 
 
// cout<<setprecision(10)<<fixed<<ans<<endl; for double



//sieve of earonsthes
// const int N = 1e7;
// bool prime[N+1];
// void sieve()
// {
//     memset(prime, true, sizeof(prime));
//     prime[0] = false;
//     prime[1] = false;
//     for(ll i = 2; i*i <= N; i++)
//     {
//         if(prime[i] == true)
//         {
//             for(ll j = i*i; j <= N; j += i)
//             {
//                 prime[j] = false;
//             }
//         }
//     }
// }
 
 
// making multiples of a number as false.
// void sieve()
// {
//     memset(prime, true, sizeof(prime));
//     prime[0] = false;
//     for(auto it : mpp)
//     {
//         if(prime[it.first] == true)
//         {
//             prime[it.first] = false;
//             for(ll j = it.first*2; j <= N; j += it.first)
//             {
//                 prime[j] = false;
//             }
//         }
//     }
// }
 

// sum of digits
// string sum_of_digits(string str)
// {
//     ll sum = 0;
//     for(ll i = 0; i < str.length(); i++)
//     {
//         sum += (str[i] - '0');
//     }
    
//     string ans = to_string(sum);
//     return ans;
// }

// count of numbers divisible by m, in the range a to b
// ll count_divisible(ll a, ll b, ll m)
// {
//     if(a % m == 0)
//     {
//         return (b/m) - (a/m) + 1;
//     }
//     else
//     {
//         return (b / m) - (a/m);
//     }
// }

// storing prime factors

// ll facts(ll n)
// {
//     ll cnt = 0;
//     for(ll d = 2; d * d <= n; d++) {
//         while(n % d == 0)
//         {
//             if(d % 2 == 0)cnt++;
//             n /= d;
//         }
//     }
//     if(n > 1 && n % 2 == 0) cnt++;
//     return cnt;
// }



signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll t;
    cin >> t;
    //sieve();
    while(t--)
    {
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        
        ll n;
        cin >> n;
        string str;
        cin >> str;
        
        vector<ll> pos;
        char mini = 'z';
        for(ll i = 0; i < str.length(); i++)
        {
            mini = min(mini, str[i]);
        }
        
        for(ll i = 0; i < str.length(); i++)
        {
            if(str[i] == mini)
            {
                pos.push_back(i+1);
            }
        }
        
        
        //for(auto it : pos) cout<<it<<" ";
        
        ll mini_ans = 1e9;
        string mini_string = "";
        for(ll i = 1; i <= 5001; i++)
        {
            mini_string += 'z';
        }
        for(ll i = 0; i < pos.size(); i++)
        {
            ll val = pos[i] - 1;
            string ans = "";
            ans += str.substr(val, n);
            if(n % 2 != val % 2)
            {
                string temp = "";
                temp = str.substr(0, val);
                reverse(temp.begin(), temp.end());
                ans += temp;
            }
            else
            {
                ans += str.substr(0, val);
            }
            

            if(ans < mini_string)
            {
                mini_string = ans;
                mini_ans = pos[i];
            } 
        }
        if(mini_ans == 1e9)
        {
            cout<<str<<endl;
            cout<<"1"<<endl;
        }
        else
        {
        cout<<mini_string<<endl;
        cout<<mini_ans<<endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction