Segmentation Fault in Suffix Array Implementation

45 Views Asked by At

when I run the following code written to implement the suffix array algorithm, I get a segmentation fault. Can anyone please help me in solving it? I think the issue is with the while since cout in all other places work properly

Following is the code is used

vector<int> suffixArray(string s, int n)
{
    vector<int> p(n); // Indexes 
    vector<int> c(n); // Classes

    {
        vector<pair<char, int>> a(n);

        for (int i = 0; i < n; i++)
        {
            a[i] = {s[i], i};
        }

        sort(a.begin(), a.end());

        for (int i = 0; i < n; i++)
            p[i] = a[i].second;
        c[p[0]] = 0;

        for (int i = 1; i < n; i++)
        {
            if (a[i].first == a[i - 1].first)
            {
                c[p[i]] = c[p[i - 1]];
            }
            else
            {
                c[p[i]] = c[p[i - 1]] + 1;
            }
        }
    }
    int k = 0;

    while (pow(2, k) < n)
    {
        vector<pair<pair<int, int>, int>> a;

        for (int i = 0; i < n; i++)
        {
            a[i] = {{c[i], c[(int)(i + pow(2, k)) % n]}, i};
        }

        sort(a.begin(), a.end());
        for (int i = 0; i < n; i++)
            p[i] = a[i].second;
        c[p[0]] = 0;

        for (int i = 1; i < n; i++)
        {
            if (a[i].first == a[i - 1].first)
            {
                c[p[i]] = c[p[i - 1]];
            }
            else
            {
                c[p[i]] = c[p[i - 1]] + 1;
            }
        }
        k++;
    }
    return p;
}

enter image description here Thank You

0

There are 0 best solutions below