Advertisement
Guest User

CH8 Biologist

a guest
Jun 20th, 2011
1,739
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.  * Email: pardal@alu.uma.es
  11.  */
  12.  
  13. public class Biologist
  14. {
  15.     private static String longestCommonSubstring( String s1, String s2 )
  16.     {
  17.         int start = 0;
  18.         int max = 0;
  19.         for ( int i = 0; i < s1.length(); i++ )
  20.         {
  21.             for ( int j = 0; j < s2.length(); j++ )
  22.             {
  23.                 int x = 0;
  24.                 while ( s1.charAt(i + x) == s2.charAt(j + x) )
  25.                 {
  26.                     x++;
  27.                     if ( (i + x >= s1.length()) || (j + x >= s2.length()) )
  28.                         break;
  29.                 }
  30.                
  31.                 if ( x > max )
  32.                 {
  33.                     max = x;
  34.                     start = i;
  35.                 }
  36.             }
  37.         }
  38.        
  39.         return s1.substring( start, start + max );
  40.     }
  41.    
  42.     private static String parseInput( String linea )
  43.     {
  44.         StringTokenizer st = new StringTokenizer( linea, " " );
  45.        
  46.         String s1 = st.nextToken();
  47.         String s2 = st.nextToken();
  48.        
  49.         return longestCommonSubstring( s1, s2 );
  50.     }
  51.    
  52.     public static void main( String[] args ) throws IOException
  53.     {
  54.         BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  55.        
  56.         while ( reader.ready() )
  57.         {
  58.             String linea = reader.readLine();
  59.             String result = parseInput( linea );
  60.            
  61.             System.out.println( result );
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement