Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:01:44+00:00 2026-05-26T02:01:44+00:00

Given two strings, S1 & S2. given scoring scheme where gap penalty, mismatch score

  • 0

Given two strings, S1 & S2. given scoring scheme where gap penalty, mismatch score and match score.

Find the S1 which have a best match with S2.

My idea is to list all possible S1 and then match one by one with S2. List all possible S1 by using brute force. Then match each possible S1 with S2 by using dp.

Is there is any faster way to do so? or suggest any reference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:01:45+00:00Added an answer on May 26, 2026 at 2:01 am

    Using Wikipedia and a little bit of thinking one could code up something like this:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #ifndef MAX
    #define MAX(a,b) ((a)>(b)?(a):(b))
    #endif
    
    #define MATCH_SCORE     2
    #define MISMATCH_SCORE  -1
    #define GAP_SCORE       0
    
    // Calculates the match score recursively.
    long MatchScore(const char* p/* in: may contain 'x' */,
                    const char* s/* in: doesn't contain 'x' */)
    {
      long res;
    
      if (*p && *s)
      {
        if ((*p == *s) ||
            ((*p == 'x') && (*s >= 'a') && (*s <= 'f')))
        {
          res = MatchScore(p + 1, s + 1) + MATCH_SCORE;
        }
        else
        {
          long s1 = MatchScore(p + 1, s + 1) + MISMATCH_SCORE;
          long s2 = MatchScore(p, s + 1) + GAP_SCORE;
          long s3 = MatchScore(p + 1, s) + GAP_SCORE;
          res = MAX(s1, MAX(s2, s3));
        }
      }
      else
      {
        res = GAP_SCORE * (long)(strlen(p) + strlen(s));
      }
    
      return res;
    }
    
    // Calculates the best matching string and the match score
    // using dynamic programming, the score must be the same
    // as returned by MatchScore().
    void FindBestMatch(const char* p/* in: may contain 'x' */,
                       const char* s/* in: doesn't contain 'x' */,
                       long* score/* out: match score */,
                       char** match/* out: best matching string */)
    {
      size_t lp = strlen(p) + 1;
      size_t ls = strlen(s) + 1;
      size_t i, j;
      long* table = (long*)malloc(lp * ls * sizeof(long));
    
      for (i = 0; i < lp; i++)
        table[0 * lp + i] = GAP_SCORE * i;
    
      for (j = 0; j < ls; j++)
        table[j * lp + 0] = GAP_SCORE * j;
    
      for (j = 1; j < ls; j++)
      {
        for (i = 1; i < lp; i++)
        {
          if ((p[i-1] == s[j-1]) ||
              ((p[i-1] == 'x') && (s[j-1] >= 'a') && (s[j-1] <= 'f')))
          {
            table[j * lp + i] = table[(j-1) * lp + (i-1)] + MATCH_SCORE;
          }
          else
          {
            table[j * lp + i] =
              MAX(table[(j-1) * lp + (i-1)] + MISMATCH_SCORE,
                  MAX(table[(j-1) * lp + i] + GAP_SCORE,
                      table[j * lp + (i-1)] + GAP_SCORE));
          }
        }
      }
    
      *score = table[lp * ls - 1];
    
      // Now, trace back the score table and construct the best matching string
    
      *match = (char*)malloc(lp);
      (*match)[lp - 1] = '\0';
    
      for (j = ls, i = lp; j || i;)
      {
        if ((p[i-1] == s[j-1]) ||
            ((p[i-1] == 'x') && (s[j-1] >= 'a') && (s[j-1] <= 'f')))
        {
          (*match)[i-1] = s[j-1];
          j--;
          i--;
        }
        else
        {
          if (table[(j-1) * lp + i] > table[j * lp + (i-1)])
          {
            j--;
          }
          else
          {
            (*match)[i-1] = p[i-1];
            i--;
          }
        }
      }
    
      free(table);
    }
    
    int main(void)
    {
      const char* pattern = "acdxdcxecxf";
      const char* str = "abdfdaaed";
      long score;
      char* match;
      char* match2;
    
      printf("pattern=\"%s\" str=\"%s\"\n", pattern, str);
      FindBestMatch(pattern, str, &score, &match);
      printf("score=%ld (recursive)\n", MatchScore(pattern, str));
      printf("score=%ld best match=\"%s\"\n", score, match);
    
      // Now repeat with the best match we've just found,
      // the result must be the same
      printf("\nRepeating with pattern=best match:\n\n");
    
      printf("pattern=\"%s\" str=\"%s\"\n", match, str);
      FindBestMatch(match, str, &score, &match2);
      printf("score=%ld (recursive)\n", MatchScore(match, str));
      printf("score=%ld best match=\"%s\"\n", score, match2);
    
      free(match);
      free(match2);
      return 0;
    }
    

    Output:

    pattern="acdxdcxecxf" str="abdfdaaed"
    score=14 (recursive)
    score=14 best match="acdfdcaecdf"
    
    Repeating with pattern=best match:
    
    pattern="acdfdcaecdf" str="abdfdaaed"
    score=14 (recursive)
    score=14 best match="acdfdcaecdf"
    

    I wonder if there’re any bugs (other than the apparent lack of error checking).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Given 2 strings, I want to find the first match of at least four
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Given the following strings 7;#User One 7;#User Two;#9;#User Two 7;#User Two;#9;#User Two;#123;#User Three I
Given two sets A and B, what is the common algorithm used to find
I have a C# .net winforms application with two forms which connects to sql
So I basically have two textboxes in the aspx file which get populated by
We have a helper function in our codebase to concatenate two (Windows) path strings:
I have two XSLT variables as given below: <xsl:variable name=staticBaseUrl select='https://www.hello.com/htapi/PrintApp.asmx/getGames?contentId=id_sudoku&uniqueId=123456&pageformat=a4' /> <xsl:variable name=dynamicUrl
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.