简体   繁体   中英

Memory allocation issue

Here is my code for the problem . The code is running fine on my Code::blocks but not on spoj site and on ideone.com.I get a runtime error. I guess the spoj server cannot allocate the required amount of memory. Please give some suggestions.

http://paste.ubuntu.com/1277109/ ( MY code )

Your code is declaring an empty string s and then is assigning to elements of it...

...
string s,res;int c=0;
int sum,carry=0;
for(int i=m-1;i>=0;i--)
{
    sum=(a[i]-'0')*2+carry;
    s[c]=sum%10+'0';         // This is undefined behavior, s is empty
    carry=sum/10;
    c++;
}
...

This is a possible answer to your problem and not your question.

I guess the question has as algorithmic flavour and its aim is to find the a solution with least time complexity (perhaps a linear time solution). It is helpful to do some prepossessing for questions related to best time complexity.

So I calculated the patterns produced for a few time steps (given below) :

step                                             pattern                         no. consecutive zero pairs

  1                                                01                                           0

  2                                               1001                                          1

  3                                             01101001                                        1

  4                                         1001011001101001                                    3

  5                                 01101001100101101001011001101001                            5

  6                 1001011001101001011010011001011001101001100101101001011001101001           11

  7                 0110100110010110100101100110100110010110011010010110100110010110           21
                    1001011001101001011010011001011001101001100101101001011001101001

  8                 1001011001101001011010011001011001101001100101101001011001101001           43
                    0110100110010110100101100110100110010110011010010110100110010110    
                    0110100110010110100101100110100110010110011010010110100110010110
                    1001011001101001011010011001011001101001100101101001011001101001

  9                 0110100110010110100101100110100110010110011010010110100110010110           85
                    1001011001101001011010011001011001101001100101101001011001101001
                    1001011001101001011010011001011001101001100101101001011001101001
                    0110100110010110100101100110100110010110011010010110100110010110
                    1001011001101001011010011001011001101001100101101001011001101001
                    0110100110010110100101100110100110010110011010010110100110010110
                    0110100110010110100101100110100110010110011010010110100110010110
                    1001011001101001011010011001011001101001100101101001011001101001

The code which produces the above patterns is given below:

#include<iostream>
using namespace std;
main()
{
string s,t=""; 
s="paste pattern produced in a time-step here";
int i,l,n=0;
l=s.length();
cout <<"s.length - "<<l<<endl;
    for(i=0;i<l;i++)
    {
        if(s[i]=='0')
          {t+="10";}
        else
          {t+="01";}
    }
l*=2;
        for(i=0;i<l-1;i++)
        {
            if(t[i]=='0' && t[i+1]=='0')
            {
            n+=1;
            }
        }
cout <<"t - "<<t<<endl;
cout <<"no. of consecutive zero pairs - "<<n<<endl;
}

Few important observations are noted below :

1)The number of characters in each time-step is double that of the previous step.

2)A pair of consecutive zeros is produced for a combination 01 in the previous time-step.

3)The second half of any pattern will be the NOT of its first half.

Now comes the interesting part. See the number of consecutive zero pair produced for each step. If we assign the result of the first step, say n as zero:

For step 2 we have the result as n*2 + 1, where n is 0.

For step 3 we have the result as n*2 - 1, where n is 1.

For step 4 we have the result as n*2 + 1, where n is 1.

For step 5 we have the result as n*2 - 1, where n is 3.

Or in general we have result equals n*2 - 1(for odd time-step) and result equals n*2 + 1(for even time-step)

This will not solve our problem as n is a variable and we need to find a mathematical formula which relates the initial result (for time-step 1) and the result at any time-step say t.

But we have an easy way out.

Look at the numbers 0,1,1,3,5,11,21,43,85....

It forms the Jacobsthal sequence .

Here is our solution.

1)Go through the input numbers and find out the maximum . This takes O(n) time.

2)Create a Look-up Table (LUT) of Jacobsthal numbers up to maximum . This takes not more than O(n) time because you just need the previous two Jacobsthal numbers for current Jacobsthal number. It is evident from the property of the Jacobsthal numbers.

3)Traverse again through the input numbers this time outputting the corresponding sequence number from the LUT. A table look-up takes O(1) time and the total time for n numbers will be O(n).

4)The time complexity of the whole problem is O(n).

One advantage of this method is that we don't have to deal with large strings.

This is just a extension of the answer from @6502.

It looks like ostringstream would be a good fit for what you want.

ostringstream oss;
string s,res;
int c=0;
int sum,carry=0;

for(int i=m-1;i>=0;i--)
{
    sum=(a[i]-'0')*2+carry;
    oss << (sum%10) << '0'; //Were you trying to concatenate a '0' as well?
    carry=sum/10;
}

s = oss.str();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM