-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.cpp
77 lines (74 loc) · 1.73 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0);cin.tie(0)
long long dp[200005][5];
int main()
{
fastio;
long long n,i,x,y;
char prv;
string s;
cin>>n;
cin>>s;
dp[0][0]=1; //won
dp[0][1]=0; //lost
for(i=1;i<n;i++)
{
if(s[i]=='R')
{
if(s[i-1]=='R')
{
dp[i][0]=dp[i-1][1]+1; //paper
dp[i][1]=dp[i-1][0]; //rock
}
else if(s[i-1]=='P')
{
dp[i][0]=dp[i-1][0]+1;
dp[i][1]=max(dp[i-1][0],dp[i-1][1]);
}
else
{
dp[i][0]=max(dp[i-1][0]+1,dp[i-1][1]+1);
dp[i][1]=dp[i-1][1];
}
}
else if(s[i]=='P')
{
if(s[i-1]=='P')
{
dp[i][0]=dp[i-1][1]+1;
dp[i][1]=dp[i-1][0];
}
else if(s[i-1]=='S')
{
dp[i][0]=dp[i-1][0]+1;
dp[i][1]=max(dp[i-1][0],dp[i-1][1]);
}
else
{
dp[i][0]=max(dp[i-1][0]+1,dp[i-1][1]+1);
dp[i][1]=dp[i-1][1];
}
}
else
{
if(s[i-1]=='S')
{
dp[i][0]=dp[i-1][1]+1;
dp[i][1]=dp[i-1][0];
}
else if(s[i-1]=='R')
{
dp[i][0]=dp[i-1][0]+1;
dp[i][1]=max(dp[i-1][0],dp[i-1][1]);
}
else
{
dp[i][0]=max(dp[i-1][0]+1,dp[i-1][1]+1);
dp[i][1]=dp[i-1][1];
}
}
}
cout<<max(dp[n-1][0],dp[n-1][1])<<"\n";
return 0;
}