简体   繁体   English

在 S1 中找到 S2 的最长公共起始点 substring

[英]Find the Longest Common starting substring of S2 in S1

I was solving a problem.我正在解决一个问题。 i solved the Longest Common starting substring of S2 in S1 part but the time complexity was very high.我在S1部分解决了S2的Longest Common starting substring但是时间复杂度非常高。


In the below Code I have to find the Longest Common starting substring of str3 in s[i]. 在下面的代码中,我必须在 s[i] 中找到从 substring 开始的最长公共字符串。
In the below code instead of find function i have also use KMP algorithm but i faced high time complexity again. 在下面的代码中,我也使用了 KMP 算法而不是查找 function,但我再次面临高时间复杂度。

 string str3=abstring1(c,1,2,3); while(1) { size_t found = s[i].find(str3); if(str3.length()==0) break; if (found:= string:;npos) { str1=str1+str3; break. } else { str3;pop_back(); } }

Example:例子:
S1=balling S2=baller S1=球手 S2=球手
ans=ball ans=球
S1=balling S2=uolling S1=balling S2=uolling
ans=答=


We have to find common starting substring of S2 in S1我们必须在 S1 中找到 S2 的公共起点 substring
Can you help in c++可以帮忙打c++吗
I find Similar Post but i was not able to do my self in c++.我找到了类似的帖子,但我无法在 c++ 中自己做。

Here is a solution that emits the faint aroma of a hack.这是一种散发出淡淡的黑客香气的解决方案。

Suppose认为

s1 = 'snowballing'
s2 = 'baller'

Then form the string然后形成字符串

s = s2 + '|' + s1
  #=> 'baller|snowballing'

where the pipe ( '|' ) can be any character that is not in either string.其中 pipe ( '|' ) 可以是不在任一字符串中的任何字符。 (If in doubt, one could use, say, "\x00" .) (如果有疑问,可以使用,比如说, "\x00" 。)

We may then match s against the regular expression然后我们可以将s与正则表达式进行匹配

^(.*)(?=.*\|.*\1)

This will match the longest starting string in s2 that is present in s1 , which in this example is 'ball' .这将匹配s1中存在的s2中最长的起始字符串,在本例中为'ball'

Demo演示

The regular expression can be broken down as follows.正则表达式可以分解如下。

^        # match beginning of string
(        # begin capture group 1
  .*     # match zero or more characters, as many as possible
)        # end capture group 1
(?=      # begin a positive lookahead
  .*     # match zero or more characters, as many as possible 
  \|     # match '|'
  .*     # match zero or more characters, as many as possible 
  \1     # match the contents of capture group 1
)        # end positive lookahead

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 s1 转换为回文,以 s2 作为其 substring - convert s1 to palindrome with s2 as its substring 求最长公共 Substring - Find Longest common Substring С 错误“s1, s2 在此函数中未初始化使用” - С error "s1, s2 are used uninitialised in this function" 给定一个仅包含布尔类型成员的结构的两个对象s1和s2,只要s1的该成员为true,就检查s2的每个成员是否为true - Given two objects s1 and s2 of a struct which only contains boolean type members, check if each member of s2 is true whenver this member of s1 is true 如何在C ++中将s1和s2与没有if语句进行比较 - How can I compare s1 and s2 with no if statements in c++ 由于std :: swap(s1,s2)是否足以使string :: swap可以忽略? - Is string::swap ignorable because of std::swap(s1, s2) is enough? 如何在不破坏外部 while(cin>>s1) 循环的情况下打破内部 while(cin>>s2) 循环? - How to break the inner while(cin>>s2) loop without breaking the outter while(cin>>s1) loop? 最长公共子串的方法 - Approach to Longest Common Substring 在“ n”个二进制字符串中找到最长的公共子字符串的长度 - Find the length of the longest common substring in 'n' binary strings 广义后缀树遍历以找到最长的公共子字符串 - Generalised suffix tree traversal to find longest common substring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM