Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
千万别看,看了就后悔#include <cstdio> #include <cstdlib> #include <set> #include <string> #include <cstdlib> #include <algorithm> #include <cstring> using namespace std; int n, nc; char data[16000007]; bool uniqueHashSet[16000007]; int randomIdx[256]; const long long MOD = 16000007; int main() { int n, nc; scanf("%d %d\n", &n, &nc); scanf("%s", data); int dataLength = strlen(data); long long sum = 0, power = 1; int idx = 0, ans = 0; int BASE = nc; // 预处理 => 离散化 for (int i = 0 ; i < dataLength; i ++) { if (randomIdx[data[i]] == 0) { randomIdx[data[i]] = ++idx; } } // 预处理 => 滚动hash power 值 for (int i = 0; i < n - 1; ++i) { power = (power * BASE) % MOD; } // => for (int i = 0; i < n; ++i) { int chRandomIdx = randomIdx[data[i]] - 1; sum = (sum * BASE + chRandomIdx) % MOD; } uniqueHashSet[sum] = true; ++ans; // => 滚动Hash计算 hash(s[i+1,i+N])= ( (hash(s[i,i+N−1]) − s[i] × Power(BASE, N − 1)) × BASE + s [i+N]) % M for(int i = n; i < dataLength; ++i) { int pewChRandomIdx = randomIdx[data[i-n]] - 1; sum = (sum - (pewChRandomIdx * power % MOD) + MOD) % MOD; int chRandomIdx = randomIdx[data[i]] - 1; sum = (sum * BASE + chRandomIdx) % MOD; if (!uniqueHashSet[sum]) { uniqueHashSet[sum] = true; ans ++; } } printf("%d\n", ans); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator