Wednesday 27 August 2014

Longest Common Subsequence in Java using Dynamic Programming

In case you find some error feel free to comment.


/*
I haven`t refactored the code for words with spaces
This Program uses Dynamic Programming Approach
*/
class LCS
{

   public static void main(String args[])
   {
      System.out.print("Enter First String : ");
 String x=System.console().readLine();
 System.out.print("Enter Second String : ");
 String y=System.console().readLine();

 char[]xArray=x.toCharArray();
 char[]yArray=y.toCharArray();

 int xSize=xArray.length;
 int ySize=yArray.length;

 int lcs[][]=new int[xSize+1][ySize+1];
 String marks[][]=new String[xSize][ySize];

 for(int j=0;j<=ySize;j++)
 {
    lcs[0][j]=0;
 }
 for(int i=0;i<=xSize;i++)
 {
   lcs[i][0]=0;
 }
 for(int i=1;i<=xSize;i++)
 {
     for(int j=1;j<=ySize;j++)
 {
    if(xArray[i-1]==yArray[j-1])
{
  lcs[i][j]=lcs[i-1][j-1]+1;
  marks[i-1][j-1]="D";
}
else if(lcs[i][j-1] >= lcs[i-1][j])
{
  lcs[i][j]=lcs[i][j-1];
  marks[i-1][j-1]="L";
}
else
{
  lcs[i][j]=lcs[i-1][j];
  marks[i-1][j-1]="U";
}
 }
 }
 /*
 System.out.println("\nLCS Matrix is : ");

 System.out.println();
 for(int i=0;i<=xSize;i++)
 {
 
   for(int j=0;j<=ySize;j++)
{
 System.out.print(lcs[i][j]+"\t");
}
System.out.println();
 }
 */

/* System.out.println("Marks Matrix is :");
 for(int i=0;i<xSize;i++)
 {
   for(int j=0;j<ySize;j++)
{
 System.out.print(marks[i][j]+"  ");
}
System.out.println();
 }
 */
 int i=xSize-1;
      int j=ySize-1;
      System.out.println("----------------------------------------------");
      int counter=lcs[xSize][ySize];
 System.out.println("Longest Common SubSequence Length is :"+counter);
 System.out.println("----------------------------------------------");
      char[]lcsArray=new char[counter];
      int fill=0;
      int seen=0;
 while(seen != counter)
 {  
     // System.out.print(marks[i][j]+"  ");
      if(marks[i][j].equals("U"))
  {
     i=i-1;
  }
  else if(marks[i][j].equals("L"))
  {
     j=j-1;
  }
  else
  {
      lcsArray[fill]=xArray[i];
  fill++;
      i=i-1;
  j=j-1;
 seen++;
  }
 
 }
 System.out.print("Longest Common SubSequence is : ");
 int lcsLength=lcsArray.length;
 for(int k=0;k<lcsLength;k++)
 {
    System.out.print(lcsArray[lcsLength-k-1]);
 }
 System.out.println();
 
   }

}

Thursday 21 August 2014

Passed Oracle Java EE 6 Web Component Developer 1z0-899 Exam

First of all thanks to Almighty Dear Allah . 

Today I took the exam and was able to pass it with 87 %. Thanks to Kathy , Bert and Bryan for writing the book for this exam. 
I am thankful to Piotr Nowicki for his great questions related to Servlet 3.0 stuff ,Nikko for his hard work in preparing questions for this exam they are really helpful . 
Big thanks to ranchers Bibeault , Tim , Ulf and list goes on. Thanks to KT for again helping me out with Credit card. 


I want to share some tips for those who are preparing for the exam. 

I read the Head First Jsp Servlet book . Did the exams given at the end of every chapter. And I was doing quite good. 
As with any other certification exam you need practice . 
Lots of material is already available here Scwcd Links , first time I used Enthuware and It is quite good. It will give you confidence to appear for the real test. Initially I find hard to remember all the API , once I made my own notes it was easy to remember them. 

My scores in enthuware test studio 

Test 1 72% 
Test 2 74% 
Test 3 82% 
Test 4 68% 
Test 5 79% 
Test 6 84% 
Test 7 79% 
Test 8 79% 
Test 9 81% 
Test 10 80% 

Don`t be lazy in making your own notes after taking mock tests. And don`t hesitate to try the code yourself and ranchers will be there to answer your silly questions. 

Regarding the real exam it gives more preference to JSTL ,Jsp standard actions , MVC , Things you can do with DD . Annotation questions was very easy , it will be easy pick for you once you go through mock exams. 
Memorize the API well if you want to score more. 

And when you feel ready , go for it . 

Knowledge should make us humble not arrogant. 
Stay silent and let your success make the noise


You can read it at coderanch also coderanch