Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<map>
- #include<algorithm>
- void gen_perm(std::map<std::string,int> &m,std::string s,int i,int n)
- {
- if(i==n-1)
- m[s]++;
- else
- {
- for(int j=i;j<n;++j)
- {
- std::swap(s[i],s[j]);
- gen_perm(m,s,i+1,n);
- std::swap(s[i],s[j]);
- }
- }
- }
- void gen_perm_stl(std::string s)
- {
- std::sort(s.begin(),s.end());
- do
- std::cout<<s<<"\n";
- while(std::next_permutation(s.begin(),s.end()));
- }
- int main()
- {
- std::string s;
- std::map<std::string,int> m;
- std::cout<<"Enter a string: ";
- std::cin>>s;
- std::cout<<"\nPermutations: \n";
- //gen_perm_stl(s); // call any one function.. stl obviously performs better
- gen_perm(m,s,0,s.length());
- for(auto it=m.begin();it!=m.end();++it)
- std::cout<<it->first<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement