avatar
毛主席说过一句话# PhotoGear - 摄影器材
g*y
1
Edit: 好像不对,还是更复杂的情况。
想了一个O(n)的解法。请帮忙看一下对不对。
if A and B have no duplicate char: easy problem. just traverse C. increase A
's
index if A's char matches or increase B's index if B's char matches.
if A and B have duplicate chars, we assume A[i, j] == B[m, n].
when we traverse C and in the position where C[k] == A[i] == B[m], we have
two ways to go: increase A's index or increase B's index. in this kind of
situation, we will always select to increase A's index.
if we are lucky, A is the right way to go, we are good.
if we select a wrong way to go, which means we should have increased B's
index, but we increased A's index. we won't find it wrong before we
reaches A[j + 1]. assume A has gone through a_l elements since i and B has
go through b_l elements since m. at this time, we know we might have
selected a wrong way. therefore, we tried to swap a_l and b_l, i.e. A's
index is set to i + b_l and B's index is set to m + a_l, i.e. n + 1. and
redo the comparisons between B[n + 1] and the current char in C. if we got a
match, we continue to traverse C; otherwise return false.
A: abe
B: abd
C: abadbe
when C reaches C[3], i.e. d, A is at the position A[2] and B is at the
position B[1]. C[3] != A[2] != B[1]
we realize that we might have selected a wrong, we swap A's index increment
and B's index increment since the last A[i] == B[j]. in this example, i = j
= 0. so A's index is set to 1 and B's index is set to 2.
def is_interleaved(astr, bstr, cstr):
"""Given three strings, a, b and c, check if c is interleaved from a and
b.
For example: a = "abcd", b = "xyz", c = "axbyzcd" => True.
Give a O(n) algorithm"""
ai, bi, ci = 0, 0, 0
dup_al, dup_bl = 0, 0
while ci < len(cstr):
if ai == len(astr) and bi == len(bstr):
return False
if ai < len(astr) and cstr[ci] == astr[ai]:
if bi != len(bstr) and astr[ai] == bstr[bi]:
dup_al += 1
ai += 1
ci += 1
elif bi < len(bstr) and cstr[ci] == bstr[bi]:
if ai != len(astr) and bstr[bi] == astr[ai]:
dup_bl += 1
bi += 1
ci += 1
elif dup_al != 0 or dup_bl != 0:
ai = ai - dup_al + dup_bl
bi = bi - dup_bl + dup_al
dup_al, dup_bl = 0, 0
else:
return False;
return ai == len(astr) and bi == len(bstr)
avatar
s*g
2
大家都来拍人体,老少爷们都摩登。
多年前在我妈的小说月报上看见的,估计是杜撰的。
大家慢慢跳,我闪。
avatar
l*a
3
sf

【在 s*****g 的大作中提到】
: 大家都来拍人体,老少爷们都摩登。
: 多年前在我妈的小说月报上看见的,估计是杜撰的。
: 大家慢慢跳,我闪。

avatar
T*t
4
摩登这个词老毛时代就有了?

【在 s*****g 的大作中提到】
: 大家都来拍人体,老少爷们都摩登。
: 多年前在我妈的小说月报上看见的,估计是杜撰的。
: 大家慢慢跳,我闪。

avatar
x*k
6
我只记得太祖讲过,凡是不以结婚为目的的谈恋爱,都是耍流氓

【在 s*****g 的大作中提到】
: 大家都来拍人体,老少爷们都摩登。
: 多年前在我妈的小说月报上看见的,估计是杜撰的。
: 大家慢慢跳,我闪。

avatar
G*Y
7
没看过摩登时代?

【在 T*******t 的大作中提到】
: 摩登这个词老毛时代就有了?
avatar
a*l
8
据说上世纪三十年代就有了......

【在 T*******t 的大作中提到】
: 摩登这个词老毛时代就有了?
avatar
a*l
9
毛主席一针见血,真是厉害

【在 s*****g 的大作中提到】
: 大家都来拍人体,老少爷们都摩登。
: 多年前在我妈的小说月报上看见的,估计是杜撰的。
: 大家慢慢跳,我闪。

avatar
l*y
10
你三天不读书, 赶不上smug了

【在 T*******t 的大作中提到】
: 摩登这个词老毛时代就有了?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。