Monster is troubling the Monk. Monk is given a task to completely traverse the string . Monster owns a string .
Monk cannot traverse the string if there is an index i in such that is a prefixof the sub-string of starting at 'i' until Monster allows him to do so.Monk is rich so he pays the monster whenever he finds such an index and Monster asks him for money.
Monster takes different amounts of money for allowing a jump at index i. Thus, you are given strings and and a cost array. The size of the array is which is same as length of .Determine the maximum amount Monster can get from Monk. By jump it means that if Monk jumps at index 'i' then he reaches index'i+|Q|'.
Note:
is guaranteed to be equal to
Monk cannot jump at any index until is a prefix of substring at index of .
If Monk doesn’t jump at any particular index he doesn’t pay anything.
Monster may(i.e it is not necessary) ask for money from Monk if is a prefix of substring at index of . He may also let him go for free if he thinks he can gain more in future.
Input:
First line contains number of testcases T.
Each testcase consists of four lines:
First line consists of string .
Second line consists of string .(Both strings consist of lowercase English characters only).
Next line contains an integer .
Next line contains an integer cost array.
Output:
Output the maximum amount Monster can get.
Constraints:
Monk cannot traverse the string if there is an index i in such that is a prefixof the sub-string of starting at 'i' until Monster allows him to do so.Monk is rich so he pays the monster whenever he finds such an index and Monster asks him for money.
Monster takes different amounts of money for allowing a jump at index i. Thus, you are given strings and and a cost array. The size of the array is which is same as length of .Determine the maximum amount Monster can get from Monk. By jump it means that if Monk jumps at index 'i' then he reaches index'i+|Q|'.
Note:
is guaranteed to be equal to
Monk cannot jump at any index until is a prefix of substring at index of .
If Monk doesn’t jump at any particular index he doesn’t pay anything.
Monster may(i.e it is not necessary) ask for money from Monk if is a prefix of substring at index of . He may also let him go for free if he thinks he can gain more in future.
Input:
First line contains number of testcases T.
Each testcase consists of four lines:
First line consists of string .
Second line consists of string .(Both strings consist of lowercase English characters only).
Next line contains an integer .
Next line contains an integer cost array.
Output:
Output the maximum amount Monster can get.
Constraints:
Explanation
In 1st case the match occurs at index . So Monster takes money from him at index and which leads to a maximum gain of .
In 2nd case there isn't any match so answer is .
In 3rd case the match occurs at index and thus Monster takes money at both these indexes and gaining amount of .
In 2nd case there isn't any match so answer is .
In 3rd case the match occurs at index and thus Monster takes money at both these indexes and gaining amount of .
Note: Your code should be able to convert the sample input into the sample output. However, this is not enough to pass the challenge, because the code will be run on multiple test cases. Therefore, your code must solve this problem statement.
Code:
import java.io.*;
import java.util.*;
public class TestClass {
public static void main(String[] args)
throws IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,ele,j,k,len;
String str,s;
for(i=0;i<n;i++)
{
str=sc.next();
len=str.length();
s=sc.next();
ele=sc.nextInt();
int arr[]=new int[ele];
List<Integer> li=new
ArrayList<Integer>();
for(j=0;j<ele;j++)
arr[j]=sc.nextInt();
int index=str.indexOf(s);
//
System.out.println(index);
if(index>=0)
{
li.add(arr[index]);
for(k=index+1;k<len;k+=index+(s.length()-1))
{
index=str.indexOf(s,k);
// System.out.println("k=
"+k+"index= "+index);
li.add(arr[index]);
}
Collections.sort(li);
System.out.println(li.get(li.size()-1)+li.get(li.size()-2));
}
else
System.out.println("0");
li.clear();
index=0;
}
}
}
Small logic change is there!!
Please do comment If u have any Queries!
Hey.. Can you please tell what's the logic change here ?
ReplyDeleteIf all the letters r same in the string...predict the behaviour n change d logic
Deletethen output will be arr[0] But that logic won't work
DeleteThe solution seems wrong
ReplyDelete1
okosirokoko
oko
11
4 2 5 11 12 4 6 3 8 2 2
Answer should be 12.
i have modified the above code.hope this will help
ReplyDeleteimport java.io.*;
import java.util.*;
public class TestClass {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine().trim());
for(int t_i=0; t_i=0)
{
//li.add(cost[index]);
max=cost[index];
for(k=index+1;k0){
int val=cost[index];
if(max<=val){
secmax=max;
max=val;
}else{
if(secmax<=val){
secmax=val;
}
}
k=index+(len2-1);
}else{
break;
}
}
result=max+secmax;
}
else
result=0;
return result;
}
}