【问题标题】:Why this program is going infinite for several inputs like in picture?为什么这个程序对于图片中的几个输入是无限的?
【发布时间】:2021-12-04 11:32:03
【问题描述】:

块引用

for _ in range(int(input())):
s1,s2,x=input(),input(),input()
l=[]
c=1
a=0
k=0
while(k<len(s1)):
    if(s1[0:k+1] in x):
        a+=1
        k+=1
        print(s1[0:k])
        l.append((s1[0:k],""))
b=0
k=0
while(k<len(s2)):
    if(s2[0:k+1] in x):
        b+=1
        k+=1
        print(s2[0:k])
        l.append(("",s2[0:k]))
for i in range(len(s1)):
    for j in range(len(s2)):
        if s1[0:i+1]+s2[0:j+1] in x and (s1[0:i+1],s2[0:j+1]) not in l:
            c+=1
            l.append((s1[0:i+1],s2[0:j+1]))
            print(s1[0:i+1]+s2[0:j+1])
            print(i,j)
print(a+b+c)
#print(a)
#print(b)
#print(c)

在这段代码中,当我传递某些输入时,它会变为无限,即 1[1: https://i.stack.imgur.com/S29Zs.png][1] 啊 bb 抗体 我卡住了 其他输入集工作正常,但这个输入集无限循环回答为什么

Sample input:

3
ab   #working fine for this input set
bc   #working fine for this input set
abc  #working fine for this input set
aa   #not working for this input set
bb   #not working for this input set
ab   #not working for this input set
aab  #working fine for this input set
acb  #working fine for this input set
bcaabacbc  #working fine for this input set

Sample output: 7 4 11

问题陈述: 给定 3 个字符串 S1、S2 和 X,求不同有序字符串对 (P,Q) 的数量,使得: 字符串 P+Q 是 X 的子字符串。字符串 P 是 S1 的某个前缀(可能为空)。字符串 Q 是 S2 的某个前缀(可能为空)。字符串的子字符串是该字符串的连续子序列。例如,“chef”是“codechef”的子字符串,但“def”不是。此外,空字符串是任何字符串的子字符串。字符串 S 的前缀是 S 的子串,出现在 S的开头。例如,“code”是“codechef”的前缀,但“chef”不是。此外,空字符串是任何字符串的前缀。“问题陈述”。在第一个 while 循环中,我正在检查字符串 s1,在第二个 while 循环中,我正在检查字符串 s2 以及 s1 和 s2 的嵌套 for 循环组合。

【问题讨论】:

  • 请提供其他可以正常工作的输入集,并解释为什么这些输入集可以正常工作。意思是......向我们展示更多具有预期输出的输入。
  • 代码应该做什么?
  • 给定 3 个字符串 S1、S2 和 X,求不同有序字符串对 (P,Q) 的数量,使得: 字符串 P+Q 是 X 的子字符串。字符串 P 是S1(可能为空)。字符串 Q 是 S2 的某个前缀(可能为空)。字符串的子字符串是该字符串的连续子序列。例如,“chef”是“codechef”的子字符串,但“def”不是。此外,空字符串是任何字符串的子字符串。字符串 S 的前缀是 S 的子字符串,出现在
  • S 的开头。例如,“code”是“codechef”的前缀,但“chef”不是。此外,空字符串是任何字符串的前缀。“问题陈述”。在第一个 while 循环中,我正在检查字符串 s1,在第二个 while 循环中,我正在检查字符串 s2 以及 s1 和 s2 的嵌套 for 循环组合。跨度>

标签: python-3.x string loops prefix infinite


【解决方案1】:

在第一个 while 循环中:while(k

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-16
    • 2021-09-29
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多