Repository navigation
Expand file tree
/
Copy pathcena.cpp
More file actions
64 lines (55 loc) · 1.3 KB
/
Copy pathcena.cpp
File metadata and controls
64 lines (55 loc) · 1.3 KB
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
#include <bits/stdc++.h>
//#include "grader.cpp"
#define MAX_S 100005
#define MAX_P 50005
#define MAX_Si 105
#define MAX_PREF 1005
int S ,P;
int s[MAX_S];
int p[MAX_P];
int first[MAX_Si][MAX_S];
int op = 0;
int pref[MAX_Si];
int dp[MAX_S][MAX_PREF];
int Solve(int i ,int pt)
{
if(pt == P)
return S-i;
if(first[p[pt]][i+1] == -1)
return 0;
if(++op < 1e7){ //check dp for current state and add its ans if it is a new state
int&ret = dp[i][pref[s[i]]];
if(ret)
return ret;
pref[p[pt]]++;
ret = Solve(first[p[pt]][i+1] ,pt+1);
pref[p[pt]]--;
return ret;
}
else //just compute the answer for this state
return Solve(first[p[pt]][i+1] ,pt+1);
}
long long conta(int _S, int _s[], int _P, int _p[]) {
S = _S ,P = _P;
for(int i=0; i<_S; i++) s[i] = _s[i];
for(int i=0; i<_P; i++) p[i] = _p[i];
memset(first ,-1 ,sizeof first);
for(int i=S-1; ~i; i--)
{
for(int j=0; j<=100; j++)
first[j][i] = first[j][i+1];
first[s[i]][i] = i;
}
pref[p[0]] = 1;
long long Ans = 0LL;
for(int i=0; i<=S-P; i++)
{
int x = 0;
if(~first[p[0]][i])
x = Solve(first[p[0]][i] ,1);
if(!x)
break;
Ans += x;
}
return Ans;
}