-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10266_KMP.cpp
More file actions
82 lines (68 loc) · 1.33 KB
/
10266_KMP.cpp
File metadata and controls
82 lines (68 loc) · 1.33 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
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 360000;
int n;
int angles[200000];
string str1, str2;
string input()
{
string str;
for (int i = 0; i < n; i++)
cin >> angles[i];
sort(angles, angles + n);
for (int i = 1; i < n; i++)
str += to_string(angles[i] - angles[i - 1]);
str += to_string(MAX + angles[0] - angles[n - 1]);
return str;
}
vector<int> get_pi()
{
int l = str2.length();
vector<int> pi(l, 0);
int j = 0;
for (int i = 1; i < l; i++)
{
while (j > 0 && str2[j] != str2[i])
j = pi[j - 1];
if (str2[j] == str2[i])
pi[i] = ++j;
}
return pi;
}
bool kmp()
{
vector<int> pi = get_pi();
int l1 = str1.length();
int l2 = str2.length();
int j = 0;
for (int i = 0; i < l1; i++)
{
while (j > 0 && str2[j] != str1[i])
j = pi[j - 1];
if (str2[j] == str1[i])
{
if (j == l2 - 1)
return 1;
j++;
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
str1 = input();
str2 = input();
str1 += str1;
if (kmp())
cout << "possible";
else
cout << "impossible";
return 0;
}