Run Length Encoding: Cpp

Home Forums Programming Run Length Encoding: Cpp

Tagged: ,

This topic contains 3 replies, has 2 voices, and was last updated by  Satish 5 years, 6 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1543

    Swathi
    Member

    The Problem is on run length encoding.

    Input : Output

    AAAAAABCCCC : 6A1B14C

    12344 : 11123124

    Source: Problem Statement

    #1544

    Satish
    Keymaster
    
    #include < iostream >
    #include < string >
    
    using namespace std;
    
    string encodeRunLength(const string &st) {
        string res, ans;
        char prev = st[0];
        int L = 0;
        for(string::const_iterator itr = st.begin(); itr != st.end(); ++itr) {
            if((prev == *itr) && L < 9){
                ++L;
            }else{
                res += static_cast<char>('0'+L);
                res += prev;
                prev = *itr;
                L = 1;
            }
        }
        res += static_cast<char>('0'+L);
        res += prev;
        char len, plen = '0';
        for(string::iterator itr = res.begin(); itr != res.end(); ++itr) {
            len = *itr++;
            if(len == '1') {
                if(plen == '1'){
                    ans += (*itr);
                }else{
                    ans += '1';
                    ans += (*itr);
                }
                if(*itr == '1') {
                    ans += '1';
                }
            }else{
                if(plen == '1') {
                    ans += plen;
                }
                ans += len;
                ans += (*itr);
            }
            plen = len;
        }
        if(plen == '1'){
            ans += '1';
        }
        return ans;
    }
    
    int main(int argc, char **argv) {    
        string st;
        while(getline(cin,st)) {
            cout << encodeRunLength(st) << endl;
        }
        return 0;
    }
    
    
    #1546

    Satish
    Keymaster

    correct the spacing between the opening < and closing >

    #1547

    Satish
    Keymaster

    Also Try this..

    
    #include<iostream>
    #include<string>
    using namespace std;
     
    string check(char c)
    {
     string s="";
    s+=c;
     if(c=='1')
     return "11";
    else 
     return s;
    }
    int main()
    {
    string s;
    while(getline(cin,s))
    {
      if(s.size())
     {
       int prevcount=0,count=1;
       char a=s[0];
      for(int i=1;i<s.size();i++)
      {
        if(s[i]==a)
       {
         count++;
         if(count==9)
         {
           cout<<9<<a;
           prevcount=9;
           if(i<s.size()-1)
          { a=s[++i];count=1;}
           else 
           count=0;
         }
       }
       else 
      { 
          if(prevcount==1)
         {
            if(count==1) 
            cout<<check(a);
             else 
           cout<<1<<count<<a;
         } 
          else 
         {  
            if(count==1)
            cout<<1<<check(a);  
            else 
            cout<<count<<a;
         }  
          prevcount=count;
        a=s[i];count=1;
      }  
       	
      }
       if(count!=0) 
       {
            if(prevcount==1)
         {
            if(count==1) 
            cout<<check(a)<<1;
             else 
           cout<<1<<count<<a;
         } 
          else 
         {  
            if(count==1)
            cout<<1<<check(a)<<1;  
            else 
            cout<<count<<a;
         }  
       }
     }
      cout<<endl;
    }
     
    return 0;
    }
    
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.