Guest User

CH8 Biologist

a guest
Jun 20th, 2011
1,890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. /*
  7.  * Tuenti Contest
  8.  * Challenge 8 - Biologist
  9.  * Author: Pedro Antonio Pardal Jimena
  10.  */
  11.  
  12. public class Biologist
  13. {
  14.     private static String longestCommonSubstring( String s1, String s2 )
  15.     {
  16.         int start = 0;
  17.         int max = 0;
  18.         for ( int i = 0; i < s1.length(); i++ )
  19.         {
  20.             for ( int j = 0; j < s2.length(); j++ )
  21.             {
  22.                 int x = 0;
  23.                 while ( s1.charAt(i + x) == s2.charAt(j + x) )
  24.                 {
  25.                     x++;
  26.                     if ( (i + x >= s1.length()) || (j + x >= s2.length()) )
  27.                         break;
  28.                 }
  29.                
  30.                 if ( x > max )
  31.                 {
  32.                     max = x;
  33.                     start = i;
  34.                 }
  35.             }
  36.         }
  37.        
  38.         return s1.substring( start, start + max );
  39.     }
  40.    
  41.     private static String parseInput( String linea )
  42.     {
  43.         StringTokenizer st = new StringTokenizer( linea, " " );
  44.        
  45.         String s1 = st.nextToken();
  46.         String s2 = st.nextToken();
  47.        
  48.         return longestCommonSubstring( s1, s2 );
  49.     }
  50.    
  51.     public static void main( String[] args ) throws IOException
  52.     {
  53.         BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  54.        
  55.         while ( reader.ready() )
  56.         {
  57.             String linea = reader.readLine();
  58.             String result = parseInput( linea );
  59.            
  60.             System.out.println( result );
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment