/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
string result ;
string str ;
int n ;
map<string,int> mp ;
int taken[40] = {0} ;
void backtrack(int pos)
{
if(result.size()==n)
{
if(!mp[result])
{
mp[result] =1 ;
cout<<result<<endl ;
}
}
for(int i =pos ; i<str.size() ; i++)
{
if(taken[i]==0)
{
if(!result.empty() && str[i]<result[result.size()-1])
continue ;
taken[i] =1 ;
result.pb(str[i]) ;
backtrack(i+1) ;
taken[i] =0 ;
result.erase(result.size()-1) ;
while(str[i]==str[i+1])
i++ ;
}
}
}
int main()
{
while(cin>>str>>n)
{
int ln =str.size() ;
sort(str.begin(),str.end()) ;
mp.clear() ;
memset(taken,0,sizeof(taken)) ;
backtrack(0) ;
}
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
string result ;
string str ;
int n ;
map<string,int> mp ;
int taken[40] = {0} ;
void backtrack(int pos)
{
if(result.size()==n)
{
if(!mp[result])
{
mp[result] =1 ;
cout<<result<<endl ;
}
}
for(int i =pos ; i<str.size() ; i++)
{
if(taken[i]==0)
{
if(!result.empty() && str[i]<result[result.size()-1])
continue ;
taken[i] =1 ;
result.pb(str[i]) ;
backtrack(i+1) ;
taken[i] =0 ;
result.erase(result.size()-1) ;
while(str[i]==str[i+1])
i++ ;
}
}
}
int main()
{
while(cin>>str>>n)
{
int ln =str.size() ;
sort(str.begin(),str.end()) ;
mp.clear() ;
memset(taken,0,sizeof(taken)) ;
backtrack(0) ;
}
}
No comments:
Post a Comment