5462. 【NOIP2017提高A组冲刺11.8】好文章
(File IO): input:article.in output:article.out
Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits
Goto ProblemSet做法:一开始还以为是字典树,然后只有50分。。(空间不够,链表打法有70),然后就字符串hash暴力啦(能水过很多字符串匹配问题诶╭(╯^╰)╮)
代码如下:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <string> 5 #include <algorithm> 6 #include <cstdlib> 7 #define LL long long 8 #define MO 1000000007 9 #define mo 1000000009 10 #define N 200009 11 #define P 61 12 #define p 97 13 using namespace std; 14 char s[N]; 15 struct arr 16 { 17 LL H, h; 18 }hash[N]; 19 LL ans, n, m; 20 LL HVAL[N], mp[N], MP[N], hval[N]; 21 22 LL Cmp(arr x, arr y) 23 { 24 if (x.H == y.H) return x.h < y.h; 25 return x.H < y.H; 26 } 27 28 void Pre_work() 29 { 30 mp[0] = 1, MP[0] = 1; 31 for (int i = 1; i <= n; i++) 32 { 33 HVAL[i] = (HVAL[i - 1] * P + (s[i] - 'a' + 1)) % MO; 34 hval[i] = (hval[i - 1] * p + (s[i] - 'a' + 1)) % mo; 35 mp[i] = (mp[i - 1] * p) % mo, MP[i] = (MP[i - 1] * P) % MO; 36 } 37 } 38 39 void Getnum(LL l, LL r, LL ain) 40 { 41 hash[ain].h = (hval[r] - hval[l - 1] * mp[r - l + 1] % mo + mo) % mo; 42 hash[ain].H = (HVAL[r] - HVAL[l - 1] * MP[r - l + 1] % MO + MO) % MO; 43 } 44 45 int main() 46 { 47 freopen("article.in", "r", stdin); 48 freopen("article.out", "w", stdout); 49 scanf("%d%d", &n, &m); 50 cin >> s + 1; 51 Pre_work(); 52 for (int i = 1; i <= n - m + 1; i++) 53 Getnum(i, i + m - 1, i); 54 sort(hash + 1, hash + n - m + 2, Cmp); 55 for (int i = 1; i <= n - m + 1; i++) 56 if (hash[i].h == hash[i + 1].h && hash[i].H == hash[i + 1].H) continue; 57 else ans++; 58 printf("%d", ans); 59 }
浙公网安备 33010602011771号