Skip to content

Instantly share code, notes, and snippets.

Created July 27, 2013 00:08
Show Gist options
  • Save anonymous/6093056 to your computer and use it in GitHub Desktop.
Save anonymous/6093056 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.Scanner;
public class DNA2
{
public static void main(String[] args) throws IOException
{
File inFile = new File("C:/CS312/dna.txt");
Scanner sc = new Scanner(inFile);
String line = sc.nextLine();
int n = Integer.parseInt(line);
for(int i = 0; i < n; i++)
{
String line1 = sc.nextLine();
String line2 = sc.nextLine();
String dna1, dna2;
if(line1.length() > line2.length())
{
dna1 = line1;
dna2 = line2;
}
else
{
dna1 = line2;
dna2 = line1;
}
System.out.println(common(dna1 , dna2));
}
sc.close();
}
public static String common(String dna1, String dna2)
{
String temp = " ";
String longest = " ";
int found;
for (int start = 0; start < dna2.length();start++)
{
for (int end = 1; end < (dna2.length() - 1); end++)
{
temp = dna2.substring(start, end);
found = dna1.indexOf(temp);
if (found != -1)
{
longest=temp;
}
}
}
return longest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment