當前位置:首頁 » 文件管理 » 壓縮的字元串

壓縮的字元串

發布時間: 2023-05-17 01:38:50

A. 字元串壓縮解壓

由於精度問題,該演算法的壓縮能力有限,字元串長度不能過長,否則會出現溢出,壓縮會出錯。還有,忘了對空格鍵處理,所以你一旦輸入空格就會結束字元串輸入

#include<iostream>
#include<math.h>
#include<string>
#include<vector>

using namespace std;

struct node
{
char elem;
double weigh;
double low;
double high;
double rang;
};

////////////////////

double value(string &code)
{
double res=0;
for(int i=0;i<code.size();i++)
{
if(code[i]=='1')
res=res+pow(2,-(i+1));
}

return res;
}

/////////////////////

int search(vector<node> &array,char &e)
{
for(int i=0;i<array.size();i++)
{
if(array[i].elem==e)
return i;
}
return -1;
}

//////////////////////

void set(string &data,vector<node> &array)
{
cin>>data;
data=data+'$';
node temp;
for(int i=0;i<data.size();i++)
{
int f=0;
for(int j=0;j<array.size();j++)
{
if(array[j].elem==data[i])
{
array[j].weigh++;
f=1;
break;
}
}
if(f==1)continue;

temp.elem=data[i];
temp.weigh=1;
array.push_back(temp);
}

array[0].low=0;
array[0].rang=array[0].weigh/data.size();
array[0].high=array[0].low+array[0].rang;
for(i=1;i<array.size();i++)
{
array[i].low=array[i-1].high;
array[i].rang=array[i].weigh/data.size();
array[i].high=array[i].low+array[i].rang;
}
}

//////////////////////

void output(vector<node> &array)
{
cout<<"elem low high rang"<<endl;
for(int i=0;i<array.size();i++)
{

cout<<array[i].elem;
cout.width(10);
cout<<array[i].low;
cout.width(10);
cout<<array[i].high;
cout.width(10);
cout<<array[i].rang<<endl;
}
}

///////////

void getarith(string &data,vector<node> &array,vector<node> &arith)
{
node temp;
int t=search(array,data[0]);
temp=array[t];
arith.push_back(temp);
for(int i=1;i<data.size();i++)
{
temp.elem=data[i];
int t=search(array,data[i]);
temp.low=arith[i-1].low+array[t].low*arith[i-1].rang;
temp.rang=arith[i-1].rang*array[t].rang;
temp.high=temp.low+temp.rang;
arith.push_back(temp);
}
}

///////////////

void code(double low,double high,string &res)
{
while(value(res)<low)
{
string temp=res+'1';
if(value(temp)>high)
res=res+'0';
else
res=temp;
}
}
////////////

void decode(double math,vector<node> &array)
{
while(1)
{
for(int i=0;;i++)
{
if(math<array[i].high)
break;
}
if(array[i].elem=='$')break;
cout<<array[i].elem;

math=math-array[i].low;
math=math/array[i].rang;
}
}

//////////////////
int main()
{
string data;
vector<node> array;
set(data,array);
string result;
vector<node> arith;

/* array[0].elem='a';
array[0].low=0;
array[0].rang=0.2;
array[0].high=0.2;

array[1].elem='b';
array[1].low=0.2;
array[1].rang=0.1;
array[1].high=0.3;

array[2].elem='c';
array[2].low=0.3;
array[2].rang=0.2;
array[2].high=0.5;

array[3].elem='d';
array[3].low=0.5;
array[3].rang=0.05;
array[3].high=0.55;

array[4].elem='e';
array[4].low=0.55;
array[4].rang=0.3;
array[4].high=0.85;

array[5].elem='f';
array[5].low=0.85;
array[5].rang=0.05;
array[5].high=0.9;

array[6].elem='$';
array[6].low=0.9;
array[6].rang=0.1;
array[6].high=1;*/

getarith(data,array,arith);

cout<<"字元數據表為:"<<endl;
output(array);

cout<<"輸入字元串的算術編碼數據表為:"<<endl;
output(arith);

string res;
code(arith[arith.size()-1].low,arith[arith.size()-1].high,res);

cout<<"字元串的算術編碼為:"<<endl;
cout<<res<<endl;

double math=value(res);
cout<<math<<endl;
cout<<endl<<"解碼結果為:"<<endl;
decode(math,array);

cout<<endl;
system("pause");
return 0;
}

B. 字元串解壓縮

剛才編成了壓縮的程序,不好意思,現在的可以解壓了。
程序沒有給你編寫讀寫文件的內容,文件讀寫自已去編,那個相對就簡單了,程序只介紹了實現基本功能的內容。你可以輸入3A4B7D測試。
void
main()
{int
m=0;int
j=0;
//string
a;
//char
c[111];
char
a[111];
char
b[111];
scanf("%s",a);
for(int
i=0;a[i]!='\0';i++)
{
cout<<"a"<
1&&a[i]-'0'<9)
{
m=a[i]-'0';}
else{b[j]=a[i];j++;}
while(m>1)
{
b[j]=a[i+1];
j++;
m--;
}
}
cout<
評論
0
0
0
載入更多

C. java字元串經過bcd壓縮後怎麼傳輸

解決方法:

據我所知,您目前的做法是:

>使用getBytes(「UTF-8」)將String轉換為位元組數組.

>壓縮位元組數組

>使用新的String(位元組,…,「UTF-8」)將壓縮位元組數組轉換為String.

>傳輸壓縮字元串

>接收壓縮字元串

>使用getBytes(「UTF-8」)將壓縮字元串轉換為位元組數組.

>解壓縮位元組數組

>使用新的String(bytes,…,「UTF-8」)將解壓縮的位元組數組轉換為String.

這種方法的問題在於步驟3.壓縮位元組數組時,您創建的位元組序列可能不再是沒汪有效的UTF-8.結果將是步驟3中的例外.

解決方案是使用像Base64這樣的「位元組到字元」編碼方案將壓縮位元組轉換為可傳輸的字元串.換句話說,用調用Base64編碼函數代替步驟3,用調用Base64解碼函數代替步驟6.

筆記:

>對於小弦,壓縮和

編碼很可能實際上

增加傳輸字元串的大小.

>如果要將壓縮的字元串合並到URL中,您可能希望為Base64選擇不同的編碼,以避免兆察緩需要進行URL轉義的字元.

>根據您傳輸的數據的性質,您可能會發現特定於域的壓縮比通用壓縮更好.考慮在創建以逗號分隔的字元串之前壓縮數據.考慮以逗號分隔的字元串的族模替代方法.

D. 壓縮演算法進行字元串壓縮

Deflater 是同時使用了LZ77演算法與哈夫曼編碼的一個無損數據壓縮演算法。

我們可以使用 java 提供的 Deflater 和 Inflater 類對 json 進行壓縮和解壓縮,下面是工具類

壓縮前的位元組長度為:1825
壓縮後的位元組長度為:284
壓縮率為63.73%,壓縮後體積為原來的36.27%

壓縮前的位元組長度為:1825
壓縮後的位元組長度為:307
壓縮率為62.04%,壓縮後體積為原來的37.95%,也是不錯的!

E. 使用C語言實現字元串的壓縮。

/*
原串:111225555
壓縮後:312245
原串:333AAAbbbb
壓縮後:333A4b
原串:ASXDCdddddd
壓縮後:1A1S1X1D1C6d
Pressanykeytocontinue
*/
#include<stdio.h>
#include<string.h>

char*CompressStr(chars[]){
chart[255];
inti=0,j,k=0;
while(s[i]){
j=i+1;
while(s[i]==s[j])++j;
t[k++]=j-i+'0';
t[k++]=s[i];
i=j;
}
t[k]='';
strcpy(s,t);
returns;
}

intmain(void){
chari,s[][20]={"111225555","333AAAbbbb","ASXDCdddddd"};
for(i=0;i<3;++i){
printf("原串:%s ",s[i]);
printf("壓縮後:%s ",CompressStr(s[i]));
}
return0;
}

F. C語言 解壓縮字元串

#include<stdio.h>

intmain()

{

chars[50],s1[100];

inti=0,j=0,k,n;

gets(s);//輸入壓縮後的字元串

while(s[i])

{

s1[j]=s[i];

//因為形式是先字元後數字,所以第一個肯定是字元,先賦入另一個數組以便後續操作。

i++;j++;//遞增,開始處理下一位的數字

n=0;

while(s[i]>='0'&&s[i]<='9')//只要是數字就要進入循環統計

{

n*=10;

n+=s[i]-'0';

i++;

}

for(k=0;k<n-1;k++)

//因為解壓縮的字元已經存入數組s1,所以只剩下n-1要再放入s1

s1[j+k]=s1[j-1];

if(n>0)//注意,一定要n>0因為有可能是連續字元,本來就沒被壓縮這時如果還把j加上n-1就反而是讓j減小了。

j+=n-1;

}

s1[j]='';//處理結束後,最後一個設為空字元結束。

printf("%s ",s1);

return0;

}

(6)壓縮的字元串擴展閱讀

C語言:數據結構-稀疏矩陣的壓縮存儲

稀疏矩陣的壓縮存儲原理,只存儲非零元素ai,j和相應的行、列序號i、j。具體方法:對稀疏矩陣中每一個非零元素設定一個三元組(i,j,ai,j)。

將所有三元組按行優先排列,組成一個三元組表(線性表)。只要存儲三元組表和該矩陣的行、列數,就能唯一確定該矩陣。

G. 壓縮字元串

#include<stdio.h>
int compress(char s[])
{
int n,k=0,count=0;
if(s[0]!=NULL)n=k+1;
while(s[n]!=NULL)
{
if(s[k]==s[n])
{n++;count++;}
else{s[++k]=s[n];n++;}
}
s[++k]='\0';
return count;
}

main()
{char num[100];
int count=0;
FILE *fp;
fp=fopen("e:\\12\\myf3.out","w");
gets(num);
count=compress(num);
fprintf(fp,"%s",num);
printf("%d",count);
fclose(fp);}
是不是還要輸學號啊#109

H. 字元串壓縮後變長

問的是字元串壓縮後變長怎麼解決嗎?辦法如下:
1、使用 StringBuilder,StringJoiner在構造時可鍵喊渣以指定一個分隔符(delimiter),然後每連接一個元素它便會加上一個delimiter。
2、使用StringJoiner,使用後formatList的代碼更加的簡潔了,滲基第一個參數為一個分隔符delimiter,第二個參稿悄數可以是一個Iterable,或者是一個變長參數的CharSequence,這樣壓縮後字元串會變短。

I. redis大key解決壓縮字元串

首先,使用Redis的大key功能,將大型字元桐飢螞串壓縮成更小的字元肢畢串,從而減少存儲空間局埋。#DISP_SEQ#2.然後,使用Redis的SET命令將壓縮.

J. 用java如何實現壓縮字元串

package javase1.day02;
/**
* 1)一種字元串壓縮演算法
* str ="aaaabbccccddeaaa"
* 壓縮為:"4a2b4c2d1e3a"
* 原理實現:
* str = "aaaabbccccddeaaa"
*
* c = str.charAt(i)//c是每個字元
* 1) 初始化
* StringBuilder buf = new StringBuilder();
* int count = 0;代表相同的字元個數
* char ch = str.charAt(0);代表正在統計的相同字元'a'
* 2) 從i=1開始迭代每個字元
* c = str.charAt(i);//c是每個當前字元
* 3) 檢查當前字元c與被統計ch是否一致
* 如果一致 count++
* 否則(不一致)
* 向緩沖區buf增加count+ch
* count=0,ch=c;
* 3)沒有下個字元就結束
* 4)還有字元串嗎?回到2)
*
* 2)實現還原演算法
* str = "4a2b4c2d1e3a";
* i
*/
public class Demo5 {
public static void main(String[] args) {
String s = comp("aaaawwwwe");
System.out.println(s);
// System.out.println(decomp(s));

}
public static String comp(String str){
int i = 1;
StringBuilder buf = new StringBuilder();
int count = 1;
char ch = str.charAt(0);
for(;;){
char c = i==str.length() ? '\10':str.charAt(i);
if(c==ch){
count++;
}else{
if(count == 1)
buf.append(ch);
else
buf.append(count).append(ch);
count=1;
ch = c;
}
i++;
if(i==str.length()+1){
break;
}
}
return buf.toString();

}
}

熱點內容
雲伺服器寬頻單位 發布:2025-02-11 20:48:11 瀏覽:538
安卓數據線公頭是哪個 發布:2025-02-11 20:45:42 瀏覽:812
網址原始密碼是什麼 發布:2025-02-11 20:33:52 瀏覽:72
怎麼創建伺服器我的世界網易 發布:2025-02-11 20:18:36 瀏覽:467
伺服器電腦與客戶端的連接 發布:2025-02-11 20:18:32 瀏覽:36
安卓哪個系統最流暢好用 發布:2025-02-11 20:17:44 瀏覽:879
蘋果平板安卓模擬器哪個好用 發布:2025-02-11 20:17:01 瀏覽:834
手機谷歌伺服器怎麼樣 發布:2025-02-11 20:08:37 瀏覽:221
編譯簡單游戲 發布:2025-02-11 20:02:10 瀏覽:866
php測評系統 發布:2025-02-11 19:42:58 瀏覽:294