c字元串壓縮
package javase1.day02;x0dx0a /**x0dx0a * 1)一種字元串壓縮演算法x0dx0a * str ="aaaabbccccddeaaa"x0dx0a * 壓縮為:"4a2b4c2d1e3a"x0dx0a * 原理實現:x0dx0a * str = "aaaabbccccddeaaa"x0dx0a * x0dx0a * c = str.charAt(i)//c是每個字元x0dx0a * 1) 初始化x0dx0a * StringBuilder buf = new StringBuilder();x0dx0a * int count = 0;代表相同的字元個數x0dx0a * char ch = str.charAt(0);代表正在統計的相同字元'a' x0dx0a * 2) 從i=1開始迭代每個字元x0dx0a * c = str.charAt(i);//c是每個當前字元x0dx0a * 3) 檢查當前字元c與被統計ch是否一致x0dx0a * 如果一致 count++x0dx0a * 否則(不一致)x0dx0a * 向緩沖區buf增加count+chx0dx0a * count=0,ch=c;x0dx0a * 3)沒有下個字元就結束x0dx0a * 4)還有字元串嗎?回到2)x0dx0a * x0dx0a * 2)實現還原演算法x0dx0a * str = "4a2b4c2d1e3a";x0dx0a * ix0dx0a */x0dx0apublic class Demo5 {x0dx0a public static void main(String[] args) {x0dx0a String s = comp("aaaawwwwe");x0dx0a System.out.println(s);x0dx0a// System.out.println(decomp(s));x0dx0a x0dx0a }x0dx0a public static String comp(String str){x0dx0a int i = 1;x0dx0a StringBuilder buf = new StringBuilder();x0dx0a int count = 1;x0dx0a char ch = str.charAt(0);x0dx0a for(;;){x0dx0a char c = i==str.length() ? '\10':str.charAt(i);x0dx0a if(c==ch){x0dx0a count++;x0dx0a }else{x0dx0a if(count == 1)x0dx0a buf.append(ch);x0dx0a else x0dx0a buf.append(count).append(ch);x0dx0a count=1;x0dx0a ch = c;x0dx0a } x0dx0a i++;x0dx0a if(i==str.length()+1){x0dx0a break;x0dx0a } x0dx0a }x0dx0a return buf.toString();x0dx0a x0dx0a }x0dx0a}
② 字元串解壓縮
剛才編成了壓縮的程序,不好意思,現在的可以解壓了。
程序沒有給你編寫讀寫文件的內容,文件讀寫自已去編,那個相對就簡單了,程序只介紹了實現基本功能的內容。你可以輸入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++怎麼壓縮字元串
一個個讀入字元,記住當前一個和上一個
如果當前和上一個相同,計數加一
不同那麼把上一個和計數放入結果字元串,上一字元值替換成當前,計數設置為1
接收到換行或者eof時結束程序並輸出
④ 字元串壓縮與解壓縮
由於精度問題,該演算法的壓縮能力有限,字元串長度不能過長,否則會出現溢出,壓縮會出錯。還有,忘了對空格鍵處理,所以你一旦輸入空格就會結束字元串輸入
#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;
}
⑤ 字元串如何壓縮
去網上搜索免費代碼,這年代還自己寫通用代碼,又浪費時間又浪費精力————前提,別拿別人的免費代碼做商業用途。
⑥ 下面的c語言字元串壓縮程序怎麼寫呀 輸入樣例 a5b3aba13b4 輸出: aaaaabbbabaaaaaaaaaaaaabbbb
#include <stdio.h>
#include <string.h>
void main()
{
char s[80];
int n=0,i=0,j;
printf("請輸入字元串:");
gets(s);
if(strlen(s)%2!=0)
{
printf("輸入有誤!");
return;
}
for(i=0;s[i]!='\0';i+=2)
{
for(j=1;j<=s[i+1]-48;j++)
printf("%c",s[i]);
}
printf("\n");
}
⑦ C語言求助:請編寫一個字元串壓縮程序,將字元串中連續出席的重復字母進行壓縮,並輸出壓縮後的字元串。
用下面的代碼給你提供個思路。這代碼連續字元不得超過9個……
#include "stdio.h"
void main(void){
char a[1000]="gcccddecc";
int i,j,k,n;
printf("Type an integer(a~z)...\nStr=");
gets(a);
for(k=i=0;a[i];i++){
for(j=i+1,n=0;a[i]==a[j];j++) n++;
if(n){
a[k++]=n+'1';
a[k++]=a[--j];
i+=n;
}
else a[k++]=a[i];
}
a[k]='\0';
printf("The result = %s.\n",a);
}
⑧ 用C語言編程 字元串原地壓縮。題目:「eeeeeaaaff" 壓縮為 "e5a3f2"把s字元串壓縮處理後結果保存在res中
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#defineMAX50
typedefstructstr
{
charch;
intccount;
structstr*next;
}node;
node*func(chara[],intn);
intmain()
{
intindex=0;
node*head,*head2,*temp;
chars[MAX],res[MAX];//最大輸入字元可以自己設置
scanf("%s",s);
head2=head=func(s,sizeof(s));
while(head)
{
res[index++]=head->ch;
res[index++]=head->ccount+48;
head=head->next;
}
res[index]='