forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhay-1.cpp
92 lines (92 loc) · 1.32 KB
/
nhay-1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 2008-07-14
// nhay-1.cpp uses Rabin-Karp. For KMP, see nhay-2.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#ifdef _MSC_VER
#define GC getchar
#else
#define GC getchar_unlocked
#endif
const int RADIX=73939133;
const int RADIX_INVERSE=2257814165;
int f(int x)
{
int result=1;
int cur=RADIX;
while (x)
{
if (x&1)
result*=cur;
x>>=1;
cur*=cur;
}
return result;
}
int main()
{
int l,j;
#ifdef _MSC_VER
freopen("nhay.in","r",stdin);
freopen("nhay.out","w",stdout);
#endif
for(;;)
{
char c=GC();
if (c==-1)
return 0;
else
ungetc(c,stdin);
scanf("%d",&l);
char* needle=new char[l+10];
char* hay=new char[l+10];
int pwr=f(l-1);
scanf("%s",needle);
GC();
int h=needle[l-1];
for (j=l-2; j>=0; j--)
h=RADIX*h+needle[j];
bool ok=true;
int cur=1;
int h2=0;
for (j=0; j<l; j++)
{
hay[j]=GC();
if (hay[j]=='\n')
{
ok=false;
break;
}
h2+=cur*hay[j];
cur*=RADIX;
}
if (!ok)
putchar('\n');
else
{
ok=false;
int pos=0;
int first=0;
for(;;)
{
if (h==h2)
{
ok=true;
printf("%d\n",pos);
}
c=GC();
if (c=='\n') break;
h2=(h2-hay[first])*RADIX_INVERSE+pwr*c;
hay[first]=c;
first++;
if (first==l)
first=0;
pos++;
}
if (!ok)
putchar('\n');
}
delete needle;
delete hay;
}
return 0;
}