This algorithm would give many duplicates if you have repeating
characters in the string.
You will have to add a hashmap/hashset or something inside the
function scope to check whether a particular character has been placed
at position n to avoid this.

On 9 May 2011 09:25, shubham <[email protected]> wrote:
> Here is the permutation generator in c++:
>
> #include<iostream>
> using namespace std;
> int count;
> void perm(string str,int n)
> {
>    if(n>=str.length()) {cout<<"("<<++count<<") "<<str<<endl;return;}
>    for(int i=n;i<str.length();i++)
>    {
>        char temp;
>        temp=str.at(i); str.at(i)=str.at(n); str.at(n)=temp;
>        perm(str,n+1);
>        temp=str.at(i); str.at(i)=str.at(n); str.at(n)=temp;
>    }
> }
> int main()
> {
>    string str;
>    cin>>str;
>    perm(str,0);
>    return 0;
> }
>
> --
> You received this message because you are subscribed to the Google Groups 
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-code?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to