0
[待解决问题]

A = 'XYZ'
B= 'YWZ'

C的格式如下：
C = 'XYWZ'

0

A = 'XYZ'
B = 'YWZ'

# 将B中的第一个字符与A中的第一个字符拼接

C = A[:1] + B[:1]

# 将B中的第三个字符与第四个字符拼接

C += B[2:]

print(C) # 输出：'XYWZ'

Technologyforgood | 园豆：6772 (大侠五级) | 2024-03-17 21:27

A = '' abcdefghijk''
B = "cdef123ghijk"

C = "abcdef123ghijk"

A = '1234567890'
B = '89abcdefu0'

C = '123456789abcdefu0‘

B = '1314abcd15167'

A = '123456'
B = '3YU456'

@等月亮爬上坡:

def concatenate_strings(A, B):
common_part = ''
for char_a, char_b in zip(A, B):
if char_a == char_b:
common_part += char_a
else:
break

``````# 找到共同部分后，将剩余部分依次拼接到一起
rest_a = A[len(common_part):]
rest_b = B[len(common_part):]

return common_part + rest_a + rest_b
``````

# 测试

A = 'abcdefghijk'
B = 'cdef123ghijk'
C = concatenate_strings(A, B)
print(C) # 输出 'abcdef123ghijk'

A = '1234567890'
B = '89abcdefu0'
C = concatenate_strings(A, B)
print(C) # 输出 '123456789abcdefu0'

@Technologyforgood: okk，感谢大佬。我这就去试试

@Technologyforgood:

0

0
``````def contact(a:str,b:str):
idx = range(len(b))
idx = reversed(idx)
idx = [i+1 for i in idx]
idx1 = 0
for i in idx:
pos1 = a.find(b[:i])
if pos1 >=0:
idx1 = i
#print('idx1:%d pos1:%d'%(idx1, pos1))
break
idx = reversed(idx)
idx2 = len(b)
for i in idx:
if i < pos1: continue
pos2 = a.rfind(b[i:])
if pos2 >=0:
idx2 = i
#print('idx2:%d pos2:%d'%(idx2, pos2))
break
x = a[:pos1]
y = a[pos1:pos2]
w = b[idx1:idx2]
z = b[idx2:]
return x + y + w + z

print(contact('abcdefghijk','cdef123ghijk'))
#abcdef123ghijk

print(contact('1234567890','89abcdefu0'))
#123456789abcdefu0

print(contact('12131415167','1314abcd15167'))
#121314abcd15167
``````
www378660084 | 园豆：368 (菜鸟二级) | 2024-04-11 20:05

您需要登录以后才能回答，未注册用户请先注册