Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- /*
- * Tuenti Contest
- * Challenge 8 - Biologist
- * Author: Pedro Antonio Pardal Jimena
- * Email: pardal@alu.uma.es
- */
- public class Biologist
- {
- private static String longestCommonSubstring( String s1, String s2 )
- {
- int start = 0;
- int max = 0;
- for ( int i = 0; i < s1.length(); i++ )
- {
- for ( int j = 0; j < s2.length(); j++ )
- {
- int x = 0;
- while ( s1.charAt(i + x) == s2.charAt(j + x) )
- {
- x++;
- if ( (i + x >= s1.length()) || (j + x >= s2.length()) )
- break;
- }
- if ( x > max )
- {
- max = x;
- start = i;
- }
- }
- }
- return s1.substring( start, start + max );
- }
- private static String parseInput( String linea )
- {
- StringTokenizer st = new StringTokenizer( linea, " " );
- String s1 = st.nextToken();
- String s2 = st.nextToken();
- return longestCommonSubstring( s1, s2 );
- }
- public static void main( String[] args ) throws IOException
- {
- BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
- while ( reader.ready() )
- {
- String linea = reader.readLine();
- String result = parseInput( linea );
- System.out.println( result );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement