POSITIVE or NEGATIVE ?
If the blood composition of the person is a subsequence of the virus composition V, then the person is identified as POSITIVE otherwise NEGATIVE.
Example:
Virus Composition, V = coronavirus
Blood Composition of the person , B = ravus
The person in question is POSITIVE as B is the subsequence of the V.
The scientists are busy with their research for medicine and request you to build a program which can quickly figure out if the person is POSITIVE or NEGATIVE. They will provide you with the virus composition V and all the people’s current blood composition. Can you help them?
Note: The virus and blood compositions are lowercase alphabet strings.
Input Format
The first line of the input consists of the virus composition, V
The second line of he input consists of the number of people, N
Next N lines each consist of the blood composition of the ith person, Bi
Constraints
1<= N <=10
1<= |B|<= |V|<= 10^5
Output Format
For each person, print POSITIVE or NEGATIVE in a separate line
Sample TestCase 1
Input
coronavirus
3
abcde
crnas
onarous
Output
NEGATIVE
POSITIVE
NEGATIVE in a separate line
Sample TestCase 1
Input
coronavirus
3
abcde
crnas
onarous
Output
NEGATIVE
POSITIVE
Answers
Answer:
import __pypy__
def main():
t=str(input())
j=int(input())
h=[]
t=t+t
for i in range(j):
u=str(input())
po=0
if(len(t)==0):
print('NEGATIVE')
elif(len(u)==0):
print('POSITIVE')
else:
for k in range(len(u)):
yu=t.find(u[k])
if(yu>=po):
po=yu
else:
print('NEGATIVE')
break
if(k>=(len(u)-1)):
print('POSITIVE')
main()
Explanation:
Program in Java
import java.util.*;
public class MyClass
{
static boolean check(String v , String p)
{
int v_len = v.length();
int p_len = p.length();
int i = 0, j = 0;
while(j < p_len && i < v_len)
{
if(v.charAt(i) == p.charAt(j))
{
j = j + 1;
}
i = i + 1;
}
if(j == p_len)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
System.out.print("Enter Virus Composition : ");
String V = Sc.next();
System.out.print("\nEnter number of people : ");
int N = Sc.nextInt();
for(int i = 1 ; i <= N ; i++)
{
System.out.print("\nEnter Blood Composition of the person : ");
String B = Sc.next();
if(check(V , B))
{
System.out.println("POSITIVE");
}
else
{
System.out.println("NEGATIVE");
}
}
}
}
Output:
Enter Virus Composition : coronavirus
Enter number of people : 3
Enter Blood Composition of the person : abcde
NEGATIVE
Enter Blood Composition of the person : crnas
POSITIVE
Enter Blood Composition of the person : onarous
NEGATIVE