BZOJ1511: [POI2006]OKR-Periods of Words

1511: [POI2006]OKR-Periods of Words

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 330  Solved: 200
[Submit][Status][Discuss]

8
babababa

24

Source

【题解】

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <algorithm>
6 #include <queue>
7 #include <vector>
8 #include <cmath>
9 #define min(a, b) ((a) < (b) ? (a) : (b))
10 #define max(a, b) ((a) > (b) ? (a) : (b))
11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
12 inline void swap(long long &a, long long &b)
13 {
14     long long tmp = a;a = b;b = tmp;
15 }
16 inline void read(long long &x)
17 {
18     x = 0;char ch = getchar(), c = ch;
19     while(ch < '0' || ch > '9') c = ch, ch = getchar();
20     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
21     if(c == '-') x = -x;
22 }
23
24 const long long INF = 0x3f3f3f3f;
25 const long long MAXN = 2000000 + 10;
26
27 char s[MAXN];
28 long long nxt[MAXN], cnt[MAXN], len;
29
30 int main()
31 {
33     nxt[0] = -1;
34     for(register long long i = 1, j = -1;i < len;++ i)
35     {
36         while(j >= 0 && s[i] != s[j + 1]) j = nxt[j];
37         if(s[i] == s[j + 1]) ++ j;
38         nxt[i] = j;
39     }
40     long long sum = 0;
41     for(register long long i = 0;i < len;++ i)
42     {
43         long long now = nxt[i];
44         if(now != -1 && cnt[now] != -1) now = cnt[now];
45         if(now != -1) sum += i - now;
46         cnt[i] = now;
47     }
48     printf("%lld", sum);
49     return 0;
50 }
BZOJ1511

posted @ 2018-01-20 15:53  嘒彼小星  阅读(136)  评论(0编辑  收藏  举报