当前位置:首页 » 文件管理 » 压缩的字符串

压缩的字符串

发布时间: 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 16:13:37 浏览:38
php的get方法 发布:2025-02-11 16:12:30 浏览:967
源码网嘉 发布:2025-02-11 16:07:06 浏览:192
免费ftp服务软件 发布:2025-02-11 15:58:06 浏览:866
大樱桃建园为什么要配置授粉树 发布:2025-02-11 15:58:00 浏览:629
五菱宏光s顶配有哪些配置 发布:2025-02-11 15:50:57 浏览:287
华为8加128配置有哪些 发布:2025-02-11 15:48:20 浏览:580
压缩机三转子 发布:2025-02-11 15:45:54 浏览:828
linux操作系统shell 发布:2025-02-11 15:45:53 浏览:339
安卓模拟器如何选择安装 发布:2025-02-11 15:34:26 浏览:177