设 s[i] 是像素序列{p1,p2,....pi}的最优分段所需存储位数
s[i] = min {s[i-k],+k*b
#include <iostream>
using namespace std;
void Compress(int P[],int S[],int l[],int size);
void TraceBack(int l[],int size);
int Length(int);
int min(int,int);
int main()
{
int P[7] = { 0,10,12,15,255,1,2 };
int S[7];
int l[7];
for (int i = 0; i < 7; i++)
{
S[i] = 0;
l[i] = 0;
}
Compress(P,S,l,6);
TraceBack(l,6);
return 0;
}
int Length(int a)
{
int l = 1;
a = a/2;
while ( a > 0)
{
l++;
a = a/2;
}
return l;
}
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
void Compress(int P[],int S[],int l[],int size)
{
int Lmax = 256;
int header = 11;
int b[size+1];
S[0] = 0;
b[0] = 0;
for(int i = 1; i <= size; i++)
{
b[i] = Length(P[i]);
int bmax = b[i];
S[i] = S[i-1]+bmax;
l[i] = 1;
int k = min(i,Lmax);
for (int j = 2; j <= k; j++)
{
if(bmax < b[i-j+1])
bmax = b[i-j+1];
if(S[i] > S[i-j] + j*bmax)
{
S[i] = S[i-j]+j*bmax;
l[i] = j;
}
}
S[i]= S[i] + header;
}
}
void TraceBack(int l[],int size)
{
while (size != 0)
{
int j = l[size];
cout << j <<" ";
size = size - j;
}
}