算法的一组元件的阵列的所有组合组合、阵列、算法、元件

2023-09-12 21:22:46 作者:我太帅了万人爱

我想要一个算法是这样的:

由于一些因素:

  A B C D电子网
 

该算法应出示包含这些元素的数组的所有组合:

  [A,B,C,D,E,F]
[AB,C,D,E,F]
[ABC,D,E,F]
[A,BC,D,E,F]
[A,B,C,DEF]
[ABCDEF]
 

无效的组合(例如):

  [AC,B,D,E,F]
[AB,BC,D,E,F]
[BC,DE,FA]
 
GEMM矩阵乘法算法中一些值得注意的点

也就是说,要素应保持秩序。

编辑: 我想使用英语句子的算法来检测复合名词。

例如:

 在桌子上是一个水壶。
 

应该承认如下词类序列。

 的代名词,确定,名词,动词,确定,名词
 

而不是

 的代名词,确定,名词,动词,确定,名词,名词
 

解决方案

我觉得这个问题可以用递归来解决。以A,B,C,D,E,F作为一个例子。我们可以结合A和B,以形成一个新的元件AB,其余的元素C,D,E,F可以是被作为同样的问题,并递归解决。此外,我们可以将A,B,C,形成一个新的元素ABC,其余元素为D,E,F。在code是如下:

 #包括<的iostream>
#包括<字符串>
#包括<载体>


使用名字空间std;

无效sequential_combination(字符输入[],诠释开始,诠释长度,矢量<串GT;&安培;结果)
{
     如果(开始> =长度)
     {
        COUT<<[;
        对(INT I = 0; I&其中; result.size() -  1 ++ⅰ)的cout&其中;&其中;导致[1]  - ;&所述;,;
        COUT<<结果[result.size() -  1];
        COUT<<];
        COUT<< ENDL;
     }
     其他
     {
         字符串preFIX =;
         的for(int i =启动; I<长度; ++ I)
         {
                 preFIX + =输入[I]
                 result.push_back(preFIX);
                 sequential_combination(输入,1 + 1,线长,结果);
                 result.pop_back();
         }
     }
}

诠释的main()
{

    字符输入[6] = {'A​​','B','C','D','E','F'};
    矢量<串GT;结果;
    sequential_combination(输入,0,6,结果);
    的getchar();
    的getchar();
    返回0;
}
 

希望它能帮助!

I want an algorithm that works like this:

Given a number of elements:

A B C D E F

The algorithm should produce all combinations of arrays containing those elements:

[A,B,C,D,E,F]
[AB,C,D,E,F]
[ABC,D,E,F]
[A,BC,D,E,F]
[A,B,C,DEF]
[ABCDEF]

Invalid combinations are (for example):

[AC,B,D,E,F]
[AB,BC,D,E,F]
[BC,DE,FA]

That is, the elements should stay in order.

EDIT: I want to use the algorithm on english sentences to detect compound nouns.

For example:

On the table is a water jug.

Should be recognised as a sequence of following word classes.

Pronoun, Determiner, Noun, Verb, Determiner, Noun

but not

Pronoun, Determiner, Noun, Verb, Determiner, Noun, Noun

解决方案

I think this problem can be solved using recursion. Take A,B,C,D,E,F as an example. We can combine A and B to form an new element AB, the remaining elements C,D,E,F can be regards as the same problem and solved recursively. Also we can combine A,B,C to form an new elements ABC and the remaining elements is D,E,F. The code is below:

#include<iostream>
#include<string>
#include<vector>


using namespace std;

void sequential_combination(char input[],int start, int length,vector<string>& result)
{
     if(start>=length)
     {
        cout<<"[";
        for(int i=0;i<result.size()-1;++i)cout<<result[i]<<",";
        cout<<result[result.size()-1];
        cout<<"]";
        cout<<endl;
     }
     else
     {
         string prefix="";
         for(int i=start;i<length;++i)
         {
                 prefix+=input[i];
                 result.push_back(prefix);
                 sequential_combination(input,i+1,length,result);
                 result.pop_back();
         }
     }     
}

int main()
{

    char input[6]={'A','B','C','D','E','F'};
    vector<string> result;
    sequential_combination(input,0,6,result);
    getchar();
    getchar();
    return 0;
}

Hope it helps!