-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmap.cpp
51 lines (46 loc) · 891 Bytes
/
map.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
# include <fstream>
# include <cstring>
# include <algorithm>
# define NR 2005
using namespace std;
ifstream f("map.in");
ofstream g("map.out");
int i,j,n,m;
int ap[NR],pi[NR],AP[NR];
char s[NR];
void KMP ()
{
int k=0,VV;
memset (pi, 0, sizeof(pi));
memset (ap, 0, sizeof(ap));
//prefixul
for (int i=2; i<=m; ++i) {
while (k>0 && s[k+1]!=s[i])
k=pi[k];
if (s[k+1]==s[i]) ++k;
pi[i]=k;
}
VV=pi[m];
while (VV) {
if (VV>=m/2+1) ++ap[VV];
else ++ap[m-VV+1];
VV=pi[VV];
}
for (int i=1; i<=m; ++i)
if (ap[i]) ++AP[i];
}
int main ()
{
f>>n>>m; f.get();
for (i=1; i<=n; ++i) {
f.getline(s+1, NR);
KMP ();
}
for (i=m/2+1; i<=m; ++i)
if (AP[i]==n) {
g<<i<<"\n";
return 0;
}
g<<m<<"\n";
return 0;
}