0

我有一个包含 10 个字符串的文件 - 每个字符串在 1 行中 - 我需要运行 LCS 并获取每个比较的 LCS 和 LCS 长度,例如,字符串 1 和字符串 2、字符串 1 和字符串 3、字符串 1 和字符串4 依此类推,直到遍历每个字符串,然后递增到字符串 2 并重复此过程,直到遍历所有字符串。

我已成功将每个字符串添加到 ArrayList 以使其更容易,但现在我在尝试将所述字符串相互比较时遇到了麻烦,我想我应该使用嵌套的 for 循环,在它通过之前我不会递增整个列表,然后递增。

任何帮助表示赞赏。这是我到目前为止的代码。

     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");
        }
4

1 回答 1

0

您使用嵌套 for 循环的方法是正确的。您可以这样做。

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
  }
}
于 2016-04-12T07:24:31.000 回答