First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

Longest Common Subsequence.

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-10405
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346

Help: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

Code:

import java.util.Scanner;
public class Uva_10405{

     public static void main(String []args){
         Scanner in=new Scanner(System.in);
     
         int c=0;
         int d=0;
         while(in.hasNext()){    
             String a=in.nextLine();
             String b=in.nextLine();
             c=a.length();
             d=b.length();
             int[][] jisan=new int[c+1][d+1];
             for(int i=1;i<=c;i++){
                 for(int j=1;j<=d;j++){
                      if(a.charAt(i-1)==b.charAt(j-1)){
                       jisan[i][j]=jisan[i-1][j-1]+1;
                      }
               else{
                     jisan[i][j]=Math.max(jisan[i-1][j],jisan[i][j-1]);
               }
                     }
                 }
                 System.out.println(jisan[c][d]);
             }
         }
   
     }

No comments:

Post a Comment