【问题标题】:Permutation Letters [closed]排列字母[关闭]
【发布时间】:2014-11-19 08:16:25
【问题描述】:

我有 2 个字母 P 和 C 以及 N 迭代数(偶数) 例如。 if N = 4=> CCPP, PPCC , CPPC, PCCP, CPCP, PCPC (想法是显示这种解决方案,其中 N= C-s 的数量 = P-s 的数量)

【问题讨论】:

  • 到目前为止,尝试了什么?请仅选择一种语言,解决方案将非常根据选择的语言而有所不同。
  • 您有问题吗?

标签: c recursion permutation


【解决方案1】:

你可以生成排列

int N;
cin >> N;
string str = string(N/2, 'C') + string(N/2, 'P');
do {
    cout << str << endl;
} while( next_permutation(str.begin(), str.end()));

Live example here

C,你需要自己写permutation function

typedef int bool;
bool true = 1;
bool false = 0;

int compare (const void *a, const void * b)
{  return ( *(char *)a - *(char *)b ); }

void swap (char* a, char* b)
{
    char t = *a;
    *a = *b;
    *b = t;
}

// This function finds the index of the smallest character
// which is greater than 'first' and is present in str[l..h]
int findCeil (char str[], char first, int l, int h)
{
    // initialize index of ceiling element
    int ceilIndex = l, i;

    // Now iterate through rest of the elements and find
    // the smallest character greater than 'first'
    for (i = l+1; i <= h; i++)
      if (str[i] > first && str[i] < str[ceilIndex])
            ceilIndex = i;

    return ceilIndex;
}
// Print all permutations of str in sorted order
void permute ( char str[] )
{
    // Get size of string
    int size = strlen(str);

    // Print permutations one by one
    bool isFinished = false;
    while ( ! isFinished )
    {
        int i;
        // print this permutation
        printf ("%s \n", str);

        // Find the rightmost character which is smaller than its next
        // character. Let us call it 'first char'
        for ( i = size - 2; i >= 0; --i )
           if (str[i] < str[i+1])
              break;

        // If there is no such chracter, all are sorted in decreasing order,
        // means we just printed the last permutation and we are done.
        if ( i == -1 )
            isFinished = true;
        else
        {
            // Find the ceil of 'first char' in right of first character.
            // Ceil of a character is the smallest character greater than it
            int ceilIndex = findCeil( str, str[i], i + 1, size - 1 );

            // Swap first and second characters
            swap( &str[i], &str[ceilIndex] );

            // Sort the string on right of 'first char'
            qsort( str + i + 1, size - i - 1, sizeof(str[0]), compare );
        }
    }
}

int main(void) {
    int N;
    char *a = NULL;
    if(1 != scanf("%d\n", &N)) {
        fprintf(stderr, "Can not read the value of N\n");
        return 1;
    }
    a = malloc(N + 1);
    if(!a) {
        fprintf(stderr, "Out of mem\n");
        return 1;
    }
    memset(a, 'C', N/2);
    memset(a + N/2, 'P', N/2);
    a[N] = '\0';
    permute(a, 0, strlen(a) - 1);
    free(a);
    return 0;
}

Live Example here

【讨论】:

  • 如何在 C 中做到这一点?
  • 请注意,将'C' 放在'P' 之前很重要,因为应该对str 进行排序以遍历所有排列。
  • 对于C,需要编写函数next_permutaion。有时间我会更新答案。
  • @Jarod42 你是对的。我从页面中链接的其他来源借用了定义。该定义似乎只适用于长度不超过 3 的字符串。我会用我自己的定义替换我错了很久,这是受 stl 算法启发的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2017-06-01
  • 1970-01-01
  • 2013-07-14
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多