[Programming problem] Fuzzy matching substring

[Programming problem] Fuzzy matching substring

Time limit: C / C ++ 1 second, 2 seconds other languages

Space limitations: C / C ++ 32M, 64M other languages

Complete string from string matching substring sub, returns the number of characters matched.

If the sub in '?' represents one to three matches any character except '\ 0' away.
If you still can not find the sub characters match, then the match is not complete.

If a complete match, returns the number of characters matched, if there are multiple matches, the return minimum number of characters that match, if not a complete match, it returns -1

 

Enter a description:

第一行输入字符串string,长度小于10000

第二行输入子串sub,长度小于100

 

Output Description:

从string开头位置完整匹配sub,匹配到的字符个数。

 

Input example 1:

abcdefg
a?c

 

Output Example 1:

3

 

Input Example 2:

aabcddefg
a?c

 

Output Example 2:

4

 

Input Example 3:

aabcddefg
b?e

 

Output Example 3:

-1

 

Input Example 4:

aabcddefg
a?d

 

Output Example 4:

5

I code implements a solution (C ++):

#include<iostream>
using namespace std;


string s1,s2;
int len1,len2;
int fun(int index1,int index2)
{
    while(index1<len1&&index2<len2)
    {
        if(s2[index2]!='?'&&s1[index1]!=s2[index2])
            return -1;

        if(index1-index2>3)
            return -1;
        if(s1[index1]==s2[index2])
        {
            index1++;
            index2++;
            continue;
        }
        if(s2[index2]=='?'&&s1[index1]!='\0')
        {
            if(s1[index1+1]==s2[index2+1])
            {
                int temp=0;
                
                if((temp=fun(index1+1,index2+1))!=-1)
                {
                    return temp;
                }
               else
                {
                   return -1;
                    //index1++;
                }
            }
            else
                index1++;
        }
    }
    if(index2!=len2)
    {
        return -1;
    }
    else
    {
        return index1;
    }


    
    
}
int main()
{
    cin>>s1>>s2;
    len1=s1.length();
    len2=s2.length();
    if(len1==0||len2==0)
    {
        cout<<0<<endl;
        return 0;
    }
    cout<<fun(0,0)<<endl;
    //getchar();
    return 0;
}

Solution two: (python)

import re

class Solution():
    def return_re_length(self, first_str, second_str):
        second_str_re = second_str.replace("?","(.*?)")
        number_wenhao = len(second_str) - len(second_str.replace("?",""))

        try:
            pattern = re.compile(r'{}'.format(second_str_re))
            res = pattern.match(first_str)
            #print(number_wenhao)

            for i in range(1,number_wenhao+1):
                if len(res.group(i)) == 0 or len(res.group(i)) > 3:
                    return -1

            return len(res.group(0))

        except:
            return -1


s1 = input()
s2 = input()
test = Solution()
print(test.return_re_length(s1, s2))

Published 61 original articles · won praise 34 · views 1799

Guess you like

Origin blog.csdn.net/qq_43162613/article/details/103932918