-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathrepeat.cpp
51 lines (44 loc) · 950 Bytes
/
repeat.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 <algorithm>
# include <cstring>
# define NR 100005
using namespace std;
ifstream f("repeat.in");
ofstream g("repeat.out");
int i,j,n,m,x,y,K,Pmin,maxx,nr;
int a[NR];
char s[NR];
bool cmp (int x, int y)
{
for (int i=0; i<K; ++i)
if (s[x+i]>s[y+i]) return 0;
else if (s[x+i]<s[y+i]) return 1;
if (x>y) return 0;
else return 1;
}
bool egal (int x, int y)
{
for (int i=0; i<K; ++i)
if (s[x+i]!=s[y+i]) return 0;
return 1;
}
int main ()
{
f.getline (s+1, NR); n=strlen(s+1);
f>>K;
n=n-K+1;
for (i=1; i<=n; ++i)
a[i]=i;
sort (a+1, a+n+1, cmp);
maxx=1; Pmin=a[1]; nr=1;
for (i=2; i<=n; ++i) {
if (egal(a[i], a[i-1])) ++nr;
else {
nr=1;
}
if (nr>maxx) maxx=nr, Pmin=a[i-nr+1];
else if (nr==maxx) Pmin=min(Pmin, a[i-nr+1]);
}
g<<maxx<<"\n"<<Pmin<<"\n";
return 0;
}