Breaking

Sunday, 1 October 2017

Monk And Monster

               Monk And Monster
                 Accolite Java Hiring Challenge

Monster is troubling the Monk. Monk is given a task to completely traverse the string P . Monster owns a string Q.
Monk cannot traverse the string P if there is an index i in P such that Q is a prefixof the sub-string of P 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 2 strings P and Q and a cost array. The size of the array is N which is same as length of P .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:
N is guaranteed to be equal to |P|
Monk cannot jump at any index until Q is a prefix of substring at index i of P.
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 Q is a prefix of substring at index i of P. 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 P.
Second line consists of string Q.(Both strings consist of lowercase English characters only).
Next line contains an integer N.
Next line contains an integer cost array.
Output:
Output the maximum amount Monster can get.
Constraints:
1T10
2|Q||P|105
N=|P|
1Cost[i]20

Sample Input


3
aaaaa
aa
5
1 6 3 10 2 
qwer
asd
4
1 2 3 4 
oksirok
ok
7
4 2 5 11 12 4 6 

Sample Output


16 
0 
8
Explanation
In 1st case the match occurs at index 1,2,3,4. So Monster takes money from him at index 2 and 4 (6+10) which leads to a maximum gain of 16.
In 2nd case there isn't any match so answer is 0.
In 3rd case the match occurs at index 1 and 6 thus Monster takes money at both these indexes and gaining amount of (4+4)=8.
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!

5 comments:

  1. Hey.. Can you please tell what's the logic change here ?

    ReplyDelete
    Replies
    1. If all the letters r same in the string...predict the behaviour n change d logic

      Delete
    2. then output will be arr[0] But that logic won't work

      Delete
  2. The solution seems wrong
    1
    okosirokoko
    oko
    11
    4 2 5 11 12 4 6 3 8 2 2

    Answer should be 12.

    ReplyDelete
  3. i have modified the above code.hope this will help



    import 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;
    }
    }

    ReplyDelete

Like