-2

Blockquote

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)

In this code when I pass certain inputs it goes infinite i.e 1[1: https://i.stack.imgur.com/S29Zs.png][1] aa bb ab I have stuck It is working fine for other inputset but looping infinitely for this input set answer why

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

Problem Statement: Given 3 strings S1, S2 and X, find the number of distinct ordered pairs of strings (P,Q) such that : String P+Q is a substring of X. String P is some prefix of S1 (possibly empty). String Q is some prefix of S2 (possibly empty). A substring of a string is a contiguous subsequence of that string. For example, "chef" is a substring of "codechef", but "def" is not. Also, an empty string is a substring of any string. A prefix of a string S is a substring of S that occurs at the beginning of S. For example, "code" is a prefix of "codechef", but "chef" is not. Also, an empty string is a prefix of any string."Problem Statement" .In first while loop I m checking in string s1 , in second while loop i m checking in string s2 and in nested for loops combinations of s1 and s2.

  • please provide other inputset that it is working fine for, and explain why it is working fine for those inputset. meaning ... show us more inputs with expected outputs. – chickity china chinese chicken Oct 16 '21 at 19:22
  • what is the code supposed to do? – chickity china chinese chicken Oct 16 '21 at 19:22
  • Given 3 strings S1, S2 and X, find the number of distinct ordered pairs of strings (P,Q) such that : String P+Q is a substring of X. String P is some prefix of S1 (possibly empty). String Q is some prefix of S2 (possibly empty). A substring of a string is a contiguous subsequence of that string. For example, "chef" is a substring of "codechef", but "def" is not. Also, an empty string is a substring of any string. A prefix of a string S is a substring of S that occurs at the – Abhishek Sanwal Oct 16 '21 at 19:30
  • beginning of S. For example, "code" is a prefix of "codechef", but "chef" is not. Also, an empty string is a prefix of any string."Problem Statement" .In first while loop I m checking in string s1 , in second while loop i m checking in string s2 and in nested for loops combinations of s1 and s2. – Abhishek Sanwal Oct 16 '21 at 19:35

1 Answers1

1

In the first while-loop which says: while(k<len(s1)), k is only incremented when the substring created by that loop exists in the x string. Since after the first iteration the substring created is "aa" which doesn't exist in the x, the value of k is not incremented. But the loop only ends when k's value is greater than the length of s1, so it never ends and gets stuck in the while-loop.