-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.cpp
58 lines (53 loc) · 971 Bytes
/
theme.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
/*
TASK:theme
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, a[5005], f[5005], ans;
void read()
{
int before, x;
scanf("%d", &n);
before = 0;
for (int i = 0; i < n; ++i)
{
scanf("%d", &x);
a[i] = x - before;
before = x;
}
a[n] = -before;
}
int main()
{
freopen("theme.in", "r", stdin);
freopen("theme.out", "w", stdout);
read();
ans = 0;
for (int fr = n - 3; fr >= 5; --fr)
{
memset(f, 0, sizeof(f));
f[0] = f[1] = 0;
for (int i = fr + 1; i <= n; ++i)
{
int j = f[i - fr];
while (j != 0 && a[i] != a[fr + j]) j = f[j];
f[i - fr + 1] = (a[i] == a[fr + j]) ? j + 1 : 0;
}
int j = 0;
for (int i = 0; i < fr - 1; ++i)
{
while (j != 0 && a[i] != a[fr + j]) j = f[j];
if (a[i] == a[fr + j])
{
j++;
ans = max(ans, j + 1);
}
}
}
if (ans < 5) printf("0\n");
else printf("%d\n", ans);
return 0;
}