Compactage / décompactage d'une chaine de caractères

Jojo092 Messages postés 136 Date d'inscription samedi 24 septembre 2005 Statut Membre Dernière intervention 12 avril 2013 - 5 nov. 2007 à 14:50
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 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...

Encore merci beaucoup de votre aide!!!!

7 réponses

Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
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;
            } 

            return sb.toString(); 
    }

    public static void main(String[] args) {
        String input = "AABNOOIPMAH";
        String output = comptage(input);

        System.out.println("Input: " + input);
        System.out.println("Output: " + output);
    }
}
0
the_wwt Messages postés 177 Date d'inscription jeudi 5 octobre 2006 Statut Membre Dernière intervention 16 janvier 2009 1
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
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
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);               

            i += 2;
            j += 2;
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String comptage_input = "AABHBVALLLAKKKA";
        String comptage_output = comptage(comptage_input);
        String decomptage_output = decomptage(comptage_output);

        System.out.println("Input comptage: " + comptage_input);
        System.out.println("Output comptage: " + comptage_output);
        System.out.println("Output decomptage: " + decomptage_output);
    }
}
0
the_wwt Messages postés 177 Date d'inscription jeudi 5 octobre 2006 Statut Membre Dernière intervention 16 janvier 2009 1
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
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 2
5 nov. 2007 à 18:41
Slt.
j'ai écri les méthode correspondant a ta demande. (!!! j'ai pas testé !!!)

public String compress (String s) {
 
 String finalString = "";
 
 String oc = "" + s.charAt(0);
 String nc = oc;
 
 int counter = 1;
 
 for (int a=1; a<s.length(); a++) {
  
  nc = "" + s.charAt(a);
  
  if (nc == oc) {
   
   counter ++;
   
  }else{
   
   finaleString += "" + oc + "" + counter;
   counter = 1;
   
  }
  
  oc = nc;
  
 }
 
 return finaleString;
 
}


 


public String uncompress (String s) {
 
 String finaleString = "";
 
 for (int a=0; a<s.lentgh(); a+=2) {
  
  String currentChar = "" +s.charAt(a);
  
  int nb = Integer.parseInt(a+1);
  
  for (int b=0; b<nb; b++) {
   
   finaleString += "" + currentChar;
   
  }
  
 }
 
 return finaleString;
 
}

voila. En espérant que ca fonctionne :p
0
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 2
5 nov. 2007 à 18:48
argn des erreur xD

(corrigées) je vien de tester, cla fonction
public String compress (String s) {
 
 String finalString = "";
 
 String oc = "" + s.charAt(0);
 String nc = oc;
 
 int counter = 1;
 
 for (int a=1; a<s.length(); a++) {
  
  nc = "" + s.charAt(a);
  
  if (oc.equals(nc)) {
   
   counter ++;
   
  }else{
   
   finalString += "" + oc + "" + counter;
   counter = 1;
   
  }
  
  oc = nc;
  
 }
 
 return finalString;
 
}


 


public String uncompress (String s) {
 
 String finalString = "";
 
 for (int a=0; a<s.length(); a+=2) {
  
  String currentChar = "" +s.charAt(a);
  
  int nb = Integer.parseInt("" + s.charAt(a+1));
  
  for (int b=0; b<nb; b++) {
   
   finalString += "" + currentChar;
   
  }
  
 }
 
 return finalString;
 
}


 


public String uncompress (String s) {
 
 String finalString = "";
 
 for (int a=0; a<s.length(); a+=2) {
  
  String currentChar = "" +s.charAt(a);
  
  int nb = Integer.parseInt("" + s.charAt(a+1));
  
  for (int b=0; b<nb; b++) {
   
   finalString += "" + currentChar;
   
  }
  
 }
 
 return finalString;
 
}

ca marche ;-) :D
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
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);               

            i += 2;
            j += 2;
        }

        return sb.toString();
    }
0
Rejoignez-nous