Thinking and optimization on the method of dp scheme recording

The general approach is to record where the state was transferred from and output recursively.

But obviously recursion has the danger of bursting the stack.

So it can be avoided by a method (that increases the difficulty of understanding and is practically useless).

Specifically, use a vector to hold the answer.

For example , in this question , the general solution is to use

void print(int x,int y)
{
    if(pre[x][y]==y)
    {
        cout<<y<<" ";
        return;
    }

    print(x-1,pre[x][y]);
    cout <<y<< "  " ; // Remember the output is in reverse order 
}

to output, and I used

int x = F;
std::vector<int> v;
for(; x >= 1; y = pre[x][y], x--)
    v.push_back(y);

reverse(v.begin(), v.end());
cout<<ans<<endl;
for(int i = 0; i < (int) v.size(); i++)
    cout<<v[i]<<" ";
puts("");

to process.

what's the function? Increase the length of the code and the difficulty of understanding (laughs)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324983598&siteId=291194637