-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1200.cpp
73 lines (53 loc) · 991 Bytes
/
1200.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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
using namespace std;
#define MAX_STRING 16000010
bool hash[MAX_STRING] = {false};
char s[MAX_STRING];
int main()
{
int n, nc;
int ascii[128];
while (scanf("%d %d", &n, &nc) != EOF)
{
memset(hash,0,sizeof(hash));
memset(ascii,0,sizeof(ascii));
scanf("%s", s);
int count = 1;
for (int i = 0; s[i] && count <= nc; i ++)
{
if (!ascii[s[i]])
{
ascii[s[i]] = count;
count ++;
}
}
int len = strlen(s) - n + 1;
int p = pow(nc, 1.0 * ( n - 1)) + 1e-8;
int counti = 0;
int sum = 0;
for (int i = 0; i < n; i ++)
{
sum = sum * nc + ascii[s[i]] - 1;
}
for (int i = 0;; i ++)
{
if (!hash[sum])
{
hash[sum] = true;
counti ++;
}
if (i >= len - 1)
{
break;
}
sum -= p * (ascii[s[i]] - 1);
sum *= nc;
sum += ascii[s[i + n]] - 1;
}
printf("%d\n", counti);
}
return 0;
}