1

文字と記号を含む約200文字の文字列があります。任意のアルゴリズムを使用してこの文字列を圧縮したいと思います...

あらゆる種類のプログラム、コード、アルゴリズムを手伝ってください

前もって感謝します

現在私はこれを使用していますが、シンボルがある場合は、範囲外の配列インデックスが表示されます。

**COMPRESSION**
byte[] encode(String txt, int bit){
int length = txt.length();
float tmpRet1=0,tmpRet2=0;
if(bit==6){
    tmpRet1=3.0f;
    tmpRet2=4.0f;
}else if(bit==5){
    tmpRet1=5.0f;
    tmpRet2=8.0f;
}
byte encoded[]=new byte[(int)(tmpRet1*Math.ceil(length/tmpRet2))];
char str[]=new char[length];
txt.getChars(0,length,str,0);
int chaVal = 0;
String temp;
String strBinary = new String("");
for (int i = 0;i<length; i++){
    temp = Integer.toBinaryString(toValue(str[i]));
    while(temp.length()%bit != 0){
        temp="0"+temp;
    }
    strBinary=strBinary+temp;
}
while(strBinary.length()%8 != 0){
   strBinary=strBinary+"0";
}
Integer tempInt =new Integer(0);
for(int i=0 ; i<strBinary.length();i=i+8){
    tempInt = tempInt.valueOf(strBinary.substring(i,i+8),2);
    encoded[i/8]=tempInt.byteValue();
}
return encoded;
}



**DECOMPRESSION** :

String decode(byte[] encoded, int bit){
String strTemp = new String("");
String strBinary = new String("");
String strText = new String("");
Integer tempInt =new Integer(0);
int intTemp=0;
for(int i = 0;i<encoded.length;i++){         
    if(encoded[i]<0){
        intTemp = (int)encoded[i]+256;
    }else
        intTemp = (int)encoded[i];
    strTemp = Integer.toBinaryString(intTemp);
    while(strTemp.length()%8 != 0){
        strTemp="0"+strTemp;
    }
    strBinary = strBinary+strTemp;
}
for(int i=0 ; i<strBinary.length();i=i+bit){
    tempInt = tempInt.valueOf(strBinary.substring(i,i+bit),2);
    strText = strText + toChar(tempInt.intValue()); 
}
return strText;
}
4

2 に答える 2

1

かつて、私が勉強しているときに、先生が私にテキストコンプレッサー(クールな宿題)をコーディングさせてくれました。基本的な考え方は、各文字が8ビットの場合、最も多く表示される文字を見つけて短い値を割り当て、表示が少ない文字には大きな値を割り当てるというものでした。

例:

A = 01010101 B = 10101010

Uncompressed: AAAB - 01010101 01010101 01010101 10101010

Compressed:

Aは3回出現します(より短い表現である必要があります)Bは1回出現します(より長い表現である必要があります)

A - 01

B - 10

Result: 01 01 01 10

したがって、別の文字と一致する可能性のある表現を持つ文字がないように、文字ごとに一連のビットを生成します。次に、生成されたスキームを圧縮ファイルに保存します。解凍する場合は、圧縮ファイルからスキームを読み取り、ビットごとに読み取りを開始します。

詳細については、こちらをご覧ください:http ://web.stonehill.edu/compsci//LC/TEXTCOMPRESSION.htm

于 2012-10-03T07:30:36.213 に答える
0

圧縮にはGZIPOutputStreamを使用し、解凍にはGZIPInputStreamを使用できます。

メモリ内で実行する場合は、上記の2つのクラスのターゲットとしてByteArrayInputStream/ByteArrayOutputStreamを使用するだけです。

以下のリンクを参照してください。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html

于 2012-10-03T07:22:55.067 に答える