Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MySize 1000

char getSubsequence(char s[], int binary, int len, char *sub)
{
    for (int j = 0; j < len; j++)
    {
        if (binary & (1 << j))
            sub[strlen(sub)] = s[j];
    }
    return sub;
}

int main()
{
    //char input[MySize] = "abc";
    //char input[MySize] = "aac";
    char input[MySize] = "aabc";

    int len = strlen(input);
    int limit = pow(2, len);
    int count[len];
    char mySub[len][limit][MySize];

    for (int i = 0; i < len; i++)
    {
        count[i] = 0;
    }
    for (int i = 1; i <= limit - 1; i++)
    {
        char sub[MySize] = "";
        int duplicate = 0;

        getSubsequence(input, i, len, sub);

        int lenOfSub = strlen(sub) - 1;
        int currentCount = count[lenOfSub];

        for(int j = 0; j < count[lenOfSub]; j++)
        {
            if(strcmp(&mySub[lenOfSub][j][0], sub) == 0)
            {
                duplicate = 1;
                break;
            }
        }
        if(!duplicate)
        {
            strcpy(&mySub[lenOfSub][currentCount][0], sub);
            count[lenOfSub]++;
        }
    }
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < count[i]; j++)
        {
            if(i == 0 && j == 0)
            {
                printf("%s", &mySub[i][j][0]);
            }
            else
            {
                printf(", %s", &mySub[i][j][0]);
            }
        }
    }

    return 0;
}