本文介绍了如何生成的所有可能的亚序列包括使用C字符串的非连续子序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,

char str[20];
str="ABCD";

输出:

1 - A, B, C,D

2 -  AB,AC, AD BC, BD, CD.

3 - ABC, ABD, BCD.

4 - ABCD.

的子序列的只在左到右的方式产生的。感谢:)

The sub-sequences are generated only in left to right fashion. Thanks :)

#include<stdio.h>
#include <conio.h>
#include<string.h>


 int sub[10];

void next(int max, int length) {

    int pos = length - 1,a;

    //find first digit that can be increased
while(pos >= 0)
{
    if(sub[pos] == max - (length - 1 - pos))
        pos--;

    else
        break;
}

    sub[pos]++; //increase digit

    //update other digits
    for( a = pos+1; a < length; a++)
        sub[a] = sub[a-1] + 1;

}

int main()
{
char word[100];
printf("Enter a word = ");
scanf("%s",word);

int max = strlen(word) - 1,n,i,a,b;


for(n=1; n <= max+1; n++)
{

    printf("\n");

    for(i = 0; i < n; i++)
    {
        sub[i] = i;
    }

    for(a = 0; ; a++)
    {
        for(b=0; b < max+1; b++)
            printf("%c",word[sub[b]]);

        printf("\n");

        if(sub[0] == max - (n - 1))
            break;

        else
            next(max, n);
    }

    printf("\n");

}


return 0;
}

此code仅产生的子序列的长度等于该字符串的长度的,它也重复一个特定的字符。

This code only produces sub-sequences of the length equal to the length of the string and it also repeats a particular character.

推荐答案

不是最干净,最有效的code,但会做:D

not the cleanest, most effective code, but this'll do :D

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

char *printForMask(int mask, char *str)
{
    int i = 0;
    int j = 0;
    char *maskedString = (char *)malloc(sizeof(char) * strlen(str) + 1);
    for (; mask > 0; mask >>= 1, j++) {
            if (mask & 1)
                    maskedString[i++] = str[j];
    }
    maskedString[i] = '\0';
    return maskedString;
}



int main()
{
    char str[20] = "ABCD";

    int mask = 1;
    int maskLen = 1;
    int stringLen = strlen(str);

    for (maskLen = 1; maskLen <= stringLen; maskLen++) {
            printf("%d ", maskLen);
            for (mask = (int)pow(2,maskLen) - 1; mask <= (int) pow(2, stringLen) - 1; mask <<= 1) {
                    char *temp = printForMask(mask, str);
                    printf ("%s%s", temp, (mask << 1 <= (int) pow(2, stringLen) - 1) ? ", " : "\n");
                    free(temp);
            }
    }
    return 0;
}

修改

字符串ABCD

str[0] = 'A'
str[1] = 'B'
str[2] = 'C'
str[3] = 'D'

吧?

所以我在做什么是使用整数的二进制重新presentation 2 ^的strlen(STR) - 1
在这种情况下将是 2 ^ 4 - 1 = 15 = 0b1111
在第一for循环的主要功能的,我增加掩模,意
开始用 =屏蔽0b0001 ,为循环的每个迭代,提高面膜 =屏蔽0b1111
而在内部循环,我却将面具,让这样的事情发生了。

So What I'm doing is using the binary representation of a integer 2^strlen(str) - 1which in this case would be 2^4 - 1 = 15 = 0b1111In the first for-loop of the main function, I increase the mask, meaningstart off with a mask = 0b0001, for each iteration of the loop, increase the mask to mask = 0b1111And in the inner for loop, I shift the mask so that something like this happens

mask = 0b0001  //A
mask = 0b0010  //B
mask = 0b0100  //C
mask = 0b1000  //D
//The inner loop will finish here, and the mask length will become 2
mask = 0b0011  //AB
mask = 0b0110  //BC
mask = 0b1100  //CD
//etc.  The string representation is made by the function printForMask()

这篇关于如何生成的所有可能的亚序列包括使用C字符串的非连续子序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:39