-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_10266.cpp
More file actions
73 lines (64 loc) · 1.31 KB
/
BOJ_10266.cpp
File metadata and controls
73 lines (64 loc) · 1.31 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
65
66
67
68
69
70
71
72
73
//21 10 16
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int arr1[200000];
int arr2[200000];
int dif_arr2[200000];
int text[400000];
int pattern[200000];
void Preprocess(void)
{
int j=0;
for (int i=1; i<n; i++)
{
while (j>0 && dif_arr2[i] != dif_arr2[j])
j = pattern[j-1];
if (dif_arr2[i] == dif_arr2[j])
pattern[i] = ++j;
}
}
bool Solve(void)
{
int j=0;
for (int i=0; i<2*n; i++)
{
while (j>0 && text[i] != dif_arr2[j])
j = pattern[j-1];
if (text[i] == dif_arr2[j])
{
j++;
if (j==n)
return true;
}
}
return false;
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >>n;
for (int i=0; i<n; i++)
cin>>arr1[i];
for (int i=0; i<n; i++)
cin>>arr2[i];
sort(arr1, arr1+n);
sort(arr2, arr2+n);
for (int j=0; j<2; j++)
{
for (int i=0; i<n-1; i++)
text[j*n+i] = arr1[i+1] - arr1[i];
text[j*n+n-1] = arr1[0]+360000-arr1[n-1];
}
for (int i=0; i<n-1; i++)
dif_arr2[i] = arr2[i+1] - arr2[i];
dif_arr2[n-1] = arr2[0]+360000-arr2[n-1];
Preprocess();
if (Solve())
cout<<"possible";
else
cout<<"impossible";
return 0;
}