This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
///Dynamic Programming
///Determine the position of a given string
int memo[500005][5][5][5];
int n,m;
int A[1000005];
//The string cannot exceed 2 between L and P, so we keep variables Max and Min
int garden(int i,int Max,int Min,int act) ///Return the number of strings in the subtree rooted i,Max,Min,act
{
if(Min<0 || Max>4 || (Max-Min)>2) //Not valid choose
return 0;
if(i==n) //Valid choose
return memo[i][Max][Min][act]=1;
if(memo[i][Max][Min][act]==0){
memo[i][Max][Min][act]=garden(i+1,max(Max,act+1),min(Min,act+1),act+1)+
garden(i+1,max(Max,act-1),min(Min,act-1),act-1);
memo[i][Max][Min][act]%=m;
}
return memo[i][Max][Min][act];
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++){
char plant;
cin>>plant;
if(plant=='L') A[i]=1;
else A[i]=2;
}
int position=0;
int Min=2,Max=2,act=2;
garden(0,2,2,2);
for(int i=0;i<n;i++){
if(A[i]==2){ //If we choose letter P, then we add all the subtree rooted next L to the answer
position+=memo[i+1][max(Max,act+1)][min(Min,act+1)][act+1];
position%=m;
Max=max(Max,act-1);
Min=min(Min,act-1);
act=act-1;
}
else{
Max=max(Max,act+1);
Min=min(Min,act+1);
act=act+1;
}
}
position=position+1; //We add one (The given string)
position%=m;
cout<<position<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |