forked from sanghaisubham/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDp+Timer.cpp
More file actions
38 lines (37 loc) · 936 Bytes
/
Dp+Timer.cpp
File metadata and controls
38 lines (37 loc) · 936 Bytes
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
#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int dp[200005][20];
int vis[200005][20]={0};
int tmr=0;
int func(int index,int hops)
{
if(index>=n)
return 0;
if(vis[index][hops]==tmr)
return dp[index][hops];
// vis[index][hops]=tmr;
int sum=0;
if(s[index]=='N')
sum=max(func(index+1,hops)-1,func(index+ 1 + (1<<(hops)),hops+1)-1);
else
sum=max(func(index+1,hops)+1,func(index+ 1 + (1<<(hops)),hops+1)-1);
vis[index][hops]=tmr;
return dp[index][hops]=sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(false);
int test;
cin>>test;
while(test--)
{
tmr++;
//memset(dp,-1,sizeof(dp));
cin>>s;
n = s.length();;
cout<<func(0,0)<<"\n";
}
}