【问题标题】:permutation of an array of strings in c++C++中字符串数组的排列
【发布时间】:2013-07-02 02:59:04
【问题描述】:

我有这个代码来做一个字符串的排列。

#include <iostream> 
#include <string.h>
using namespace std;

/* Prototipo de función */
void Permutaciones(char *, int l=0); 
void sort(string scadena[]);
//array global to copy all permutations and later sort

string array[900000];
int m=0;
int main() {
int casos;
cin>>casos; 
char palabra[casos][13];
for(int i=0;i<casos;i++)
    cin>>palabra[i];

for(int i=0;i<casos;i++){
    m=0;
    Permutaciones(palabra[i]);
    sort(array);
}

  sort(array);
system("pause");
return 0;
}

void sort(string scadena[]){
string temp;

for(int i=0;i<m;i++){
    for(int j=i+1;j<m;j++){
        if(scadena[i]>scadena[j]){
            temp=scadena[i];
            scadena[i]=scadena[j];
            scadena[j]=temp;    
        }           
    }
}

for(int i=0;i<m;i++){
    for(int j=1;j<m;j++){
        if(scadena[i]==scadena[j] && j!=i){
            for(int k=j;k <m; k++){
                scadena[k]=scadena[k+1];
            }
            m--;
            j--;
        }

    }
}   
for(int i=0;i<m;i++){
    cout<<scadena[i]<<endl;
}   
}


void Permutaciones(char * cad, int l) {
char c;    /* variable auxiliar para intercambio */
int i, j;  /* variables para bucles */
int n = strlen(cad);

for(i = 0; i < n-l; i++) {
  if(n-l > 2){
    Permutaciones(cad, l+1);
  } 
  else {
        array[m]=cad;
        m++;            
  }
  /* Intercambio de posiciones */
  c = cad[l];
  cad[l] = cad[l+i+1];
  cad[l+i+1] = c;
  if(l+i == n-1) {
     for(j = l; j < n; j++){
        cad[j] = cad[j+1];
     } 
     cad[n] = 0;
  }
 }
}

代码生成所有排列都很好,后来对数组进行了排序,它工作正常。但是当我打算删除重复的字符串时,代码会显示一些重复的内容,而不是排序。

谁能告诉我我的错误是什么?

【问题讨论】:

  • 使用调试器或类似的东西,逐行执行代码。无论如何,这都是一个很好的锻炼。
  • 我是农业工程师,我不懂调试器
  • 无意冒犯,但您的评论刚刚出现在我的个人 Stackoverflow 名言列表中。 “我是一名农业工程师。我不懂调试器。”漂亮。
  • 有人认为这个问题与另一个名字相似的用户的another question 非常相似吗?
  • 但是说真的,调试器是一种用于调试的工具,即帮助从代码中删除错误(如:问题/错误)。它的主要功能之一是它可以让你“逐步”执行程序,通常大致对应“逐行”,同时观察变量的值如何变化。

标签: c++ arrays string function


【解决方案1】:

您可以使用标准库更轻松地完成它:

#include <algorithm>

using namespace std;

int main() {
    int a[] = {1, 2, 5, 6, 7};
    int n = 5;
    do {
// print array a
    } while (next_permutation(a, a + n));
}

除非任务是您自己实现它。当然,在您尝试以这种方式排列之前,请确保您的数组已排序,否则您会错过一些排列。

【讨论】:

  • +1。不过,我会使用std::vector 并跳过using
【解决方案2】:

HERE,是一个最简单的代码,用于生成给定数组的所有组合/排列,不包括一些特殊库(仅包括 iostream.hstring)并且不包括使用一些特殊的命名空间(仅使用 namespace std)。

void shuffle_string_algo( string ark )
{

//generating multi-dimentional array:

char** alpha = new char*[ark.length()];
for (int i = 0; i < ark.length(); i++)
    alpha[i] = new char[ark.length()];

//populating given string combinations over multi-dimentional array
for (int i = 0; i < ark.length(); i++)
    for (int j = 0; j < ark.length(); j++)
        for (int n = 0; n < ark.length(); n++)
            if( (j+n) <= 2 * (ark.length() -1) )
                if( i == j-n)
                    alpha[i][j] = ark[n];
                else if( (i-n)== j)
                    alpha[i][j] = ark[ ark.length() - n];

if(ark.length()>=2)
{
    for(int i=0; i<ark.length() ; i++)
    {
        char* shuffle_this_also = new char(ark.length());
        int j=0;
        //storing first digit in golobal array ma
        ma[v] = alpha[i][j];

        //getting the remaning string
        for (; j < ark.length(); j++)
            if( (j+1)<ark.length())
                shuffle_this_also[j] = alpha[i][j+1];
            else
                break;
        shuffle_this_also[j]='\0';

        //converting to string
        string send_this(shuffle_this_also);

        //checking if further combinations exist or not
        if(send_this.length()>=2)
        {
            //review the logic to get the working idea of v++ and v--
            v++;
            shuffle_string_algo( send_this);
            v--;
        }
        else
        {
            //if, further combinations are not possiable print these combinations 
            ma[v] = alpha[i][0];
            ma[++v] = alpha[i][1];
            ma[++v] = '\0';
            v=v-2;

            string disply(ma);
            cout<<++permutaioning<<":\t"<<disply<<endl;
        }
    }
}
}

主要

int main()
{
string a;
int ch;
do
{
    system("CLS");
    cout<<"PERMUNATING BY ARK's ALGORITH"<<endl;
    cout<<"Enter string: ";
    fflush(stdin);
    getline(cin, a);

    ma = new char[a.length()];

    shuffle_string_algo(a);

    cout<<"Do you want another Permutation?? (1/0): ";
    cin>>ch;
} while (ch!=0);

return 0;
}

希望!它可以帮助你!如果您对理解逻辑有疑问,请在下面评论,我会编辑。

【讨论】:

    猜你喜欢
    • 2014-07-15
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多