C++ C++;,如果某些条目重复,则输出所有可能的排列(递归算法)

C++ C++;,如果某些条目重复,则输出所有可能的排列(递归算法),c++,arrays,algorithm,sorting,recursion,C++,Arrays,Algorithm,Sorting,Recursion,问题 时间限制:1.00秒内存限制:512 MB 给定一个字符串,您的任务是生成可以使用其字符创建的所有不同字符串 输入 唯一的输入行具有长度为n的字符串。每个字符位于a–z之间 输出 然后按字母顺序打印k行:字符串。 整数k:字符串的数目 约束条件 1.≤N≤八, 代码 #include<bits/stdc++.h> using namespace std; void fun(int c[],int &x, string s,int l,int n) { //T

问题

时间限制:1.00秒内存限制:512 MB

给定一个字符串,您的任务是生成可以使用其字符创建的所有不同字符串

输入

唯一的输入行具有长度为n的字符串。每个字符位于a–z之间

输出

然后按字母顺序打印k行:字符串。 整数k:字符串的数目

约束条件 1.≤N≤八,

代码

#include<bits/stdc++.h>
using namespace std;


void fun(int c[],int &x, string s,int l,int n)
{
    //To use a Recursive Algorithms to find all the n! ways
    if(l==n)
    {
        // in all the n! ways this below loop is to impose the 
        // order in the terms which were same but considered different 
        // in aaabc all the 3 a's are in the positions 0 , 1 , 2 so
        // but the below  Recursive Algorithms considers it to be 
        // a1a2a3 so this below loops is to place an order in them 
        int t=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(s[c[i]]==s[c[j]]) {if(c[i]>c[j]){t++; break;} }
                if(t>0) break;
            }
            if(t>0) break;
        }

        // c[n] array is going to take the position of the characters
        // in the elements in s  
        // if not assigned the value would be 10
        if(t==0) { x++;
        for(int i=0;i<n;i++) { cout<<s[c[i]];}
        cout<<endl;}
    }

    else
    {
        for(int i=0;i<n;i++)
        {
            if(c[i]<10)continue;
            c[i]=l;
            fun(c,x,s,l+1,n);
            c[i]=10;
        }
    }
}



int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    string s; cin>>s;
    int n = s.length();

// 10 is not going to disturb any of the positioning because 
// maximum value of the is 8 
    int c[n]={10};
    int x=0;

    fun(c,x,s,0,n);
    cout<<x;
}
#包括
使用名称空间std;
void fun(int c[],int&x,字符串s,int l,int n)
{
//使用递归算法来查找所有的n!方法
如果(l==n)
{
//在所有n!种方式中,下面的循环都是为了施加
//按相同但被视为不同的条款排序
//在aaabc中,所有3个a都处于位置0、1、2,因此
//但是下面的递归算法认为它是
//a1a2a3所以下面的循环是对它们下订单
int t=0;
对于(int i=0;i0)中断;
}
如果(t>0)中断;
}
//c[n]数组将占据字符的位置
//在s元素中
//如果未指定,则该值为10
如果(t==0){x++;

对于(int i=0;i您忘记了您老师的电子邮件,因此我们可以直接向他们发送您的家庭作业!作为这里的新用户,请使用并阅读。此外,对于像您这样的家庭作业问题,请先付出一些努力。特别是,这意味着将任务简化为问题,而不是您编写的全部代码。此外,请确保您格式化您的代码始终保持一致,因此更易于阅读、理解和发现代码中的错误。