Search code examples
calgorithmsubsequence

My program for printing longest palindrome subsequence isn't working, When I run it,console windows stops working after taking input


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

int ai, aj;                     // ai and aj to store the value of i and j respectively
int maxx(int a, int b) {        // to return max of the two numbers
    return (a <= b) ? b : a;
}

void LongestPal(char a[], int n) {    // to find longest palindrome
    int i, j, max = 0;
    int p[1000][1000] = { 0 };

    for (j = 0; j < n; j++)
        for (i = 0; i <= j; i++) {
            if (i == j) {       // for one string having only one character
                p[i][j] = 1;
                if (max < p[i][j]) {
                    max = p[i][j];
                    ai = i;
                    aj = j;
                }
            }
            if (j == (i + 1)) {   // for string having two characters
                if (a[i] == a[j]) {  // if the string is like "aa","bb" etc.
                    p[i][j] = 2;
                    if (max < p[i][j]) {
                        max = p[i][j];
                        ai = i;
                        aj = j;
                    }
                } else {       // if string like "ab","ba" etc.
                    p[i][j] = 1;
                    if (max < p[i][j]) {
                        max = p[i][j];
                        ai = i;
                        aj = j;
                    }
                }
            } else {        // for all other type of strings
                if (a[i] == a[j]) {   // if a longer palindrome found
                    p[i][j] = p[i-1][j-1] + 2;
                    if (max < p[i][j]) {
                        max = p[i][j];
                        ai = i;
                        aj = j;
                    }
                } else {   // if no longer palindrome is present
                    p[i][j] = maxx(p[i+1][j], p[i][j-1]);
                    if (max < p[i][j]) {
                        max = p[i][j];
                        ai = i;
                        aj = j;
                    }
                }
            }
        }
}

int main() {
    int i, j, n;
    char a[1000];

    printf("Just enter the string hoss!\n");
    scanf("%s", &a);
    n = strlen(a);
    LongestPal(a, n);
    for (i = ai; i <= aj; i++)
        printf("%c", a[i]);

    return 0;
}

In this program I wanna find longest palindrome Subsequence, but unable to run program

I have written comments for each case

This program for printing longest palindrome subsequence isn't working, When I run it, the Windows console stops working after taking input.


Solution

  • Your program fails because it allocates too much data with automatic storage (aka on the stack). int p[1000][1000] uses 4MB, probably too much for your system default stack size. You can try and use less space by allocating this array as:

    int p[n][n];
    

    This is allowed in C99, but your compiler might not support C99.

    Your algorithm is a little complicated. Why not enumerate all positions for i and j and just verify with an auxiliary function if you have a palindrome there and keep track of the longest one:

    #include <stdio.h>
    #include <string.h>
    
    int isPalindrome(const char *a, int i, int j) {
        for (; i < j; i++, j--) {
            if (a[i] != a[j])
                return 0;
        }
        return 1;
    }
    
    void getLongestPalindrome(const char *a, int n, int *ai, int *aj) {
        int i, j, maxi, maxj;
    
        for (maxi = maxj = i = 0; i < n; i++) {
            for (j = n - 1; j > i + maxj - maxi; j--) {
                if (isPalindrome(a, i, j)) {
                    maxi = i;
                    maxj = j;
                    break;
                }
            }
        }
        *ai = maxi;
        *aj = maxj;
    }
    
    int main(void) {
        char a[1000];
        int i, j;
    
        printf("Enter the string: ");
        if (scanf("%999s", a) == 1) {
            getLongestPalindrome(a, strlen(a), &i, &j);
            printf("longest palindrome at %d..%d: %.*s\n", i, j, j - i + 1, a + i);
        }
        return 0;
    }