LightOj 1023 Discovering Permutations Solution



Problem Link :https://lightoj.com/problem/discovering-permutations 

Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
int ans=0;
void permute(string a, int l, int r,int k)
{
    if(ans>k)
   return ;
    if ((l == r)&&(ans<=k))
    {
        ans++;
        if(ans<=k)
            cout<<a<<endl;
    }
    else
    {
        for (int i = l; i <= r; i++)
        {
            swap(a[l], a[i]);
            permute(a, l+1, r,k);
        }
    }
}
int main()
{
    long long n,t,i,k,r,ts=1;
    string s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    cin>>t;
    while(t--)
    {
        ans=0;
        cin>>n>>k;
        cout<<"Case "<<ts<<":"<<endl;
        ts++;
        string a;
        for(i=0; i<n; i++)
        {
            a+=s[i];
        }
        permute(a,0,n-1,k);
    }
    return 0;
}

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.