'How to find the longest sequence of strings in order, but not necessarily contiguous
I have a program that reads input from two text files:
answersA.txt
10
US Independence
French Revolution
WW I
Great Depression
WW II
Korean War
British Invasion
Vietnam War
Gulf War
Dot Com Era
myanswers1.txt
7
Korean War
British Invasion
WW I
Vietnam War
Great Depression
US Independence
French Revolution
The first line is the how many values there will be. I'm trying to find the longest pattern between the two files. For these cases, the longest pattern is "Korean War,British Invasion,Vietnam War" and the score from the grade() method would be 5, since there were 5 answers right. I'm trying to use the dynamic programming approach, and although I can figure out how to do this for a singular string, I am struggling to figure out how to do it for an array of strings (if that's even the best way to handle it).
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Patterns {
public int grade;
public String LHP;
String[] answers = null;
String[] myAnswers = null;
public Patterns(String filename) {
List<String> readAnswers = new ArrayList<String>();
try {
FileInputStream newFile = new FileInputStream(filename);
DataInputStream data_input = new DataInputStream(newFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
if ((line.length() != 0)) {
readAnswers.add(line);
}
}
buffer.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(readAnswers);
answers = new String[readAnswers.size()];
answers = readAnswers.toArray(answers);
}
public int grade(String filename) {
List<String> readMyAnswers = new ArrayList<String>();
try {
FileInputStream newFile = new FileInputStream(filename);
DataInputStream data_input = new DataInputStream(newFile);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String line;
while ((line = buffer.readLine()) != null) {
if ((line.length() != 0)) {
readMyAnswers.add(line);
}
}
buffer.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(readMyAnswers);
myAnswers = new String[readMyAnswers.size()];
myAnswers = readMyAnswers.toArray(myAnswers);
grade = LCS(answers, myAnswers);
return grade;
}
public String pattern(String filename) {
return LHP;
}
public static int LCS(String[] A, String[] B) {
int[][] LCS = new int[A.length + 1][B.length + 1];
String[][] solution = new String[A.length + 1][B.length + 1];
for (int i = 0; i <= B.length; i++) {
LCS[0][i] = 0;
solution[0][i] = "0";
}
for (int i = 0; i <= A.length; i++) {
LCS[i][0] = 0;
solution[i][0] = "0";
}
for (int i = 1; i <= A.length; i++) {
for (int j = 1; j <= B.length; j++) {
if (A[i - 1] == B[j - 1]) {
LCS[i][j] = LCS[i - 1][j - 1] + 1;
} else {
LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
}
return LCS[A.length][B.length];
}
public static void main(String[] args) {
Patterns test1 = new Patterns("answersA.txt");
System.out.println("The score is: " + test1.grade("myanswers1.txt"));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
