C# Shortest Superstring Problem Algorithms

C# Shortest Superstring Problem Algorithms

Today, We want to share with you C# Shortest Superstring Problem Algorithms.
In this post we will show you Shortest Superstring Problem in C#, hear for C# Program to find all substrings in a string we will give you demo and example for implement.
In this post, we will learn about Implementation of Shortest Common Supersequence Problem in C# with an example.

Shortest Superstring Problem

In this post, we will learn about Shortest Superstring Problem with an example.

Now in this post, I will explain Shortest Superstring Problem with appropriate example in C#.Net.For a given set of n strings myArr[], find the smallest string that contains each string in the given set as substring. Shortest Superstring Problem is a NP Hard problem. A solution which finds shortest superstring takes exponential time.

READ :  Todo Smart Task schedule Lists using Vuejs Examples

Here,Two strings are considered as overlapping if prefix of one string is same suffix of other string or vice verse. The vmaximum overlap mean length of the matching prefix and suffix is vmaximum.

Input:  myArr[] = {"geeks", "quiz", "for"}
Output: geeksquizfor

Input:  myArr[] = {"catg", "ctaagt", "gcta", "ttca", "atgcatc"}
Output: gctaagttcatgcatc

Shortest Superstring Problem using C++

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;

int findOverlappingPair(string s1, string s2, string& str)
{
    int vmax = INT_MIN;
    int m = s1.length();
    int n = s2.length();

    for (int i = 1; i <= min(m, n); i++)
    {
        if (s1.compare(m - i, i, s2, 0, i) == 0)
        {
            if (vmax < i)
            {
                //update vmax and str
                vmax = i;
                str = s1 + s2.substr(i);
            }
        }
    }

    for (int i = 1; i <= min(m, n); i++)
    {
        if (s1.compare(0, i, s2, n - i, i) == 0)
        {
            if (vmax < i)
            {
                vmax = i;
                str = s2 + s1.substr(i);
            }
        }
    }

    return vmax;
}

string findShortestSuperstring(vector<string> myArr)    
{
    int n = myArr.size();

    while (n != 1)
    {
        int vmax = INT_MIN;

        int p, q;
        string res_str;

        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                string str;

                int r = findOverlappingPair(myArr[i], myArr[j], str);

                if (vmax < r)
                {
                    vmax = r;
                    res_str.assign(str);
                    p = i, q = j;
                }
            }
        }

        n--;

        if (vmax == INT_MIN)
            myArr[0] += myArr[n];
        else
        {
            myArr[p] = res_str;

            myArr[q] = myArr[n];
        }
    }

    return myArr[0];
}

// Shortest Superstring Problem
int main()
{
    vector<string> myArr = { "CATGC", "CTAAGT", "GCTA", "TTCA", "ATGCATC" };

    cout << "The Good Shortest Superstring is " << findShortestSuperstring(myArr);

    return 0;
}

Read :

READ :  Angular Multipart/form-data File Upload using http post method

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about C# Shortest Superstring Problem Algorithms.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

Your email address will not be published. Required fields are marked *