Wednesday, 31 October 2012

RUN LENGTH ENCODING


SPOJ Problem Set (classical)

1787. Run Length Encoding

Problem code: ENCONDIN



Your task is to write a program that performs a simple form of run-length encoding, as described by the rules below.

Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.

Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped with a 1, thus two 1 characters are output.

Input Specification

The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input.

Output Specification

Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.

Sample Input

AAAAAABCCCC
12344
Sample Output

6A1B14C
11123124

SOLUTION---



#include
#include
int main()
{
    char str[10000];
    while(gets(str))
    {
        char encoding[20000];
        int k=0,prev_count=0,count=0,i,len;
        char ch;
        len=strlen(str);
        for(i=0;i<len;i++)
        {
            prev_count=count;
            count=1;
            if(str[i]==str[i+1]&&(i+1)<len)
            {
                count=2;
                ch=str[i];
                i=i+2;
                while(str[i]==ch)
                {
                    i=i+1;
                    count=count+1;
                    if(count==9)
                    break;
                }
                i=i-1;
            }
            if(count!=1)
            {
                encoding[k]=count+'0';
                k=k+1;
                encoding[k]=str[i];
                k=k+1;
            }
            else if(count==1)
            {
                if(count!=prev_count){
                    if(str[i]!='1')
                    {
                    encoding[k]='1';
                    k=k+1;
                    encoding[k]=str[i];
                    k=k+1;
                    encoding[k]='1';
                    k=k+1;
                    }
                    else if(str[i]=='1')
                    {
                    encoding[k]='1';
                    k=k+1;
                    encoding[k]='1';
                    k=k+1;
                    encoding[k]='1';
                    k=k+1;
                    encoding[k]='1';
                    k=k+1;
                    }
                }
                else
                {
                    if(str[i]!='1')
                    {
                        k=k-1;
                        encoding[k]=str[i];
                        k=k+1;
                        encoding[k]='1';
                        k=k+1;
                    }
                    else if(str[i]=='1')
                    {
                        encoding[k]='1';
                        k=k+1;
                        encoding[k]='1';
                        k=k+1;
 
                    }
                }
            }
        }
        for(i=0;i<k;i++)
            printf("%c",encoding[i]);
        printf("\n");
    }
    return 0;
}

1 comment:

Arnab Dutta said...

Question:
-------- 
Find the maximum rectangle (in terms of area) under a histogram in the most optimal way. This means to find the area of largest rectangle that can be extracted from the Histogram.

Answer:
------
/**
*
* @author arnab
*/
public class MaxRectangleInHistogram {

Stack S;
StackItem curr;
StackItem maxRectangle;

public StackItem getMaxRectangleInHistogram(int A[], int n){
int i = 0;
S = new Stack();
S.push(new StackItem(0,0,-1));
maxRectangle = new StackItem(0,0,-1);

while(i S.peek().height){
S.push(curr);
}else if(curr.height == S.peek().height){
S.peek().sup = i+1;
}else if(curr.height < S.peek().height){

while((S.size()>1) && (curr.height<=S.peek().height)){
curr.sub = S.peek().sub;
S.peek().sup = i;
decideMaxRectangle(S.peek());
S.pop();
}
S.push(curr);
}
i++;
}

while(S.size()>1){
S.peek().sup = i;
decideMaxRectangle(S.peek());
S.pop();
}

return maxRectangle;
}

private void decideMaxRectangle(StackItem s){

if(s.getArea() > maxRectangle.getArea() )
maxRectangle = s;
}

}

Explanation:
-----------
Please download the PDF from

http://www.scribd.com/doc/112600829/Rectangle-of-Max-AREA-in-Histogram