Search code examples
javaalgorithmlcs

LCS algorithm running through a file with 10 strings


I have a file with 10 strings - each string in 1 line - and I need to run LCS and get the LCS and LCS length of each comparison, for example, String 1 with String 2, String 1 with String 3, String 1 with String 4 and so on until it goes through each string and then it increments to String 2 and repeats this process until it goes through all of them.

I have successfully added each string to an ArrayList to make it easier but now I'm having trouble trying to compare said strings to one another, I am thinking that I should use a nested for loop where i doesn't increment until it goes through the entire list, then it increments.

Any help is appreciated. And this is the code I have so far.

     public static void main(String[] args) {

        List<String> Collection = new ArrayList<>();
        String FirstLine = null;
        int i;

        File Temp1 = new File("CollectionSeqs/listSeqs-consensustest-errorhigh-l10.nsol_win.txt");

        try{
            InputStream fis = new FileInputStream(Temp1);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));

            for (String line = br.readLine(); line != null; line = br.readLine()) {
                Collection.add(line);
                System.out.println(line);
             }
            br.close();
        }
        catch(Exception e){
            System.err.println("Error: Target File Cannot Be Read");
        }

Solution

  • You are right in your approach to use nested for loop.Here is how You can do this.

    for(int i=0;i<Collection.size();++i)
    {
      String s1=Collection.get(i);
      for(int j=i+1;j<Collection.size();++j)
      {
        String s2=Collection.get(j);
        run the LCS for string s1 and s2
      }
    }