Compactage / décompactage d'une chaine de caractères
Jojo092
Messages postés136Date d'inscriptionsamedi 24 septembre 2005StatutMembreDernière intervention12 avril 2013
-
5 nov. 2007 à 14:50
Ombitious_Developper
Messages postés2333Date d'inscriptionsamedi 28 février 2004StatutMembreDernière intervention26 juillet 2013
-
5 nov. 2007 à 20:12
Bonjour tout le monde,
j'ai un gros problème. J'ai un TP sur le compactage / décompactage d'une chaine de caractères en utilisant la méthode RLE. Malheureusement, je ne sais pas par où commencer, si quelqu'un pourrait m'apporter son aide, ca serait fort aimable de votre part ^^
Je vous donne un exemple de ce que doit faire l'algorithme :
chaine d'entrée E = ABYYYY555ZZZZZAAW....
chaine de sortie S = A1B1Y453Z5A2W1...
Ombitious_Developper
Messages postés2333Date d'inscriptionsamedi 28 février 2004StatutMembreDernière intervention26 juillet 201338 5 nov. 2007 à 16:25
Salut:
public class Test {
public static String comptage(String input) {
if (input == null)
throw new NullPointerException();
if (input.equals(""))
return "";
StringBuffer sb = new StringBuffer();
int i = 0;
while (i < input.length()) {
char c = input.charAt(i);
sb.append("" + c);
int j = i + 1;
while (j < input.length() && input.charAt(j) == c)
++j;
sb.append("" + (j - i));
i = j;
}
the_wwt
Messages postés177Date d'inscriptionjeudi 5 octobre 2006StatutMembreDernière intervention16 janvier 20091 5 nov. 2007 à 16:30
Bonjour,
tu t'es pas trop fais mal aux neurones toi avant de poster cette question...
Pour implémenter un algorithme il faut savoir le faire avec un stylo.
A première vue ta super méthode de compression regarde s'il y a des suites de caractères et les remplaces ainsi:
BBBB
B4
C'est pas top mais si c'est l'exercice...
Dis nous ce que tu attends ...
Cordialement,
Pierrick
Ombitious_Developper
Messages postés2333Date d'inscriptionsamedi 28 février 2004StatutMembreDernière intervention26 juillet 201338 5 nov. 2007 à 17:06
Salut:
Pierrick >> Je m'exerce.
Voici l'exemple complet:
public class Test {
public static String comptage(String input) {
if (input == null)
throw new NullPointerException();
if (input.equals(""))
return "";
StringBuffer sb = new StringBuffer();
int i = 0;
while (i < input.length()) {
char c = input.charAt(i);
sb.append("" + c);
int j = i + 1;
while (j < input.length() && input.charAt(j) == c)
++j;
sb.append("" + (j - i));
i = j;
}
return sb.toString();
}
public static String decomptage(String input) {
if (input == null)
throw new NullPointerException();
if (input.length()%2 != 0)
throw new IllegalArgumentException();
if (input.equals(""))
return "";
StringBuffer sb = new StringBuffer();
int i = 0;
int j = 1;
while(i < input.length() - 1) {
char c = input.charAt(i);
char nbre = input.charAt(j);
if (Character.isDigit(c))
throw new IllegalArgumentException();
if (!Character.isDigit(nbre) || nbre == '0')
throw new IllegalArgumentException();
for (int k = 0; k < Integer.parseInt("" + nbre); k++)
sb.append("" + c);
the_wwt
Messages postés177Date d'inscriptionjeudi 5 octobre 2006StatutMembreDernière intervention16 janvier 20091 5 nov. 2007 à 17:58
Salut [auteur/OMBITIOUSDEVELOPPER/237842.aspx Ombitious_Developper,]
on doit se méprendre car ta chaine compressée fait 20 caractères et décompressée 15...
C'est on ne peut moins lent... mdr
Pierrick
Vous n’avez pas trouvé la réponse que vous recherchez ?
Ombitious_Developper
Messages postés2333Date d'inscriptionsamedi 28 février 2004StatutMembreDernière intervention26 juillet 201338 5 nov. 2007 à 20:12
Salut:
Petite rectification de la méthode decomptage:
public static String decomptage(String input) {
if (input == null)
throw new NullPointerException();
if (input.length()%2 != 0)
throw new IllegalArgumentException();
if (input.equals(""))
return "";
StringBuffer sb = new StringBuffer();
int i = 0;
int j = 1;
while(i < input.length() - 1) {
char c = input.charAt(i);
char nbre = input.charAt(j);
// A supprimer pour prendre en compte de digits
//if (Character.isDigit(c))
// throw new IllegalArgumentException();
if (!Character.isDigit(nbre) || nbre == '0')
throw new IllegalArgumentException();
for (int k = 0; k < Integer.parseInt("" + nbre); k++)
sb.append("" + c);