import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int cases = 1;
String m = "";
while ((m = br.readLine()) != null) {
if (m.equals("#")) {
break;
}
sb.append(String.format("%2d. ", cases));
cases++;
String str = br.readLine();
if (m.equals("") || str.equals("")) {
sb.append("Blank!\n");
continue;
}
StringBuilder temp = new StringBuilder();
LinkedList<String> list=new LinkedList<String>();
for (int i = 0; i < m.length(); i++) {
char c = m.charAt(i);
if (c >= 'a' && c <= 'z') {
temp.append(c);
} else if (c >= 'A' && c <= 'Z') {
temp.append(c);
} else if (c >= '0' && c <= '9') {
temp.append(c);
} else {
if (!temp.toString().equals("")) {
list.add(temp.toString());
temp = new StringBuilder();
}
}
}
if (!temp.toString().equals("")) {
list.add(temp.toString());
}
LinkedList<String> list2=new LinkedList<String>();
temp = new StringBuilder();
int counter = 0;
HashSet<String> tempSet = new HashSet<String>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'a' && c <= 'z') {
temp.append(c);
} else if (c >= 'A' && c <= 'Z') {
temp.append(c);
} else if (c >= '0' && c <= '9') {
temp.append(c);
} else {
if (!temp.toString().equals("")) {
list2.add(temp.toString());
temp = new StringBuilder();
}
}
}
if (!temp.toString().equals("")) {
list2.add(temp.toString());
}
String[]arr=new String[list.size()],arr2=new String[list2.size()];
int index=0;
while(!list.isEmpty()){
arr[index]=list.remove();
index++;
}
index=0;
while(!list2.isEmpty()){
arr2[index]=list2.remove();
index++;
}
sb.append("Length of longest match: ").append(LCS(arr, arr2)).append("\n");
}
System.out.print(sb);
}
static int LCS(String[] m, String[] n) {
int[][] arr = new int[m.length + 1][n.length + 1];
for (int i = 0; i < m.length + 1; i++) {
arr[i][0] = 0;
}
for (int i = 0; i < n.length + 1; i++) {
arr[0][i] = 0;
}
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++) {
if (m[i].equals(n[j])) {
arr[i + 1][j + 1] = arr[i][j] + 1;
}else{
arr[i+1][j+1]=Math.max(arr[i+1][j],arr[i][j+1]);
}
}
}
return arr[m.length][n.length];
}
}
No comments:
Post a Comment