[英]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但是时间复杂度非常高。
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'
。
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.