Problème sous-chaine commune plus longue
Bonjour!
Je dois bosser sur ceci
À date j'ai réalisé toutes les parties de l'algo
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| private static void genereMatrice() {
matriceLongueurs = new int[sequence1.length() + 1][sequence2.length() + 1];
for (int i = 1; i <= sequence1.length(); i++) {
for (int j = 1; j <= sequence2.length(); j++) {
if (sequence1.charAt(i - 1) == sequence2.charAt(j - 1)) {
matriceLongueurs[i][j] = 1 + matriceLongueurs[i - 1][j - 1];
} else {
matriceLongueurs[i][j] = Math.max(matriceLongueurs[i - 1][j], matriceLongueurs[i][j - 1]);
}
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
private static String backTrack(String s1, String s2, int i, int j)
{
if (i == 0 || j == 0)
return "";
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
return backTrack(s1, s2, i - 1, j - 1) + s1.charAt(i-1);
}
else if (matriceLongueurs[i - 1][j] > matriceLongueurs[i][j - 1]) {
return backTrack(s1, s2, i - 1, j);
}
else {
return backTrack(s1, s2, i, j - 1);
}
} |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
private static void printDiff(int i, int j) {
if(i>0 &&j>0 &&sequence1.charAt(i-1)==sequence2.charAt(j-1)){
printDiff(i-1,j-1);
Pep8.charo(sequence1.charAt(i-1));
}
else{
if(j>0&&(i==0||matriceLongueurs[i][j-1]>=matriceLongueurs[i-1][j])){
printDiff(i-1,j-1);
Pep8.stro("-"+sequence2.charAt(j-1));
}
else if(i>0&&(j==0||matriceLongueurs[i][j-1]<=matriceLongueurs[i-1][j])){
printDiff(i-1,j-1);
Pep8.charo(sequence1.charAt(i-1));
}
}
} |
Là où j'ai un problème c'est lorsque un caractère de la séquence1 n'est pas dans la séquence2 je voudrais afficher un tiret '-'
Par exemple
Séquence1 ABCDE
Séquence2 ACDC
Et mon programme devrait afficher
Alors que ça m'affiche :
après 3h de bidouillage, je galère toujours
Des idées sur ce qui cloche dans ma logique ?
Merci d'avance ;)