题目描述
输入格式
输出格式
样例
数据范围与提示
思路分析 : hash 拉链即可解决
代码示例:
using namespace std; #define ll unsigned long long const ll maxn = 5e6+5; const ll mm = 200007; char s[maxn]; ll k, len; ll p = 19873; ll hash_[maxn], pp[maxn]; void init_hash() { hash_[0] = 0; pp[0] = 1; for(ll i = 1; i <= len; i++) { hash_[i] = hash_[i-1]*p + (s[i]-'a'); pp[i] = pp[i-1]*p; } } struct node { ll x; ll cnt = 0; int next; //!!! }arr[maxn]; int head[mm+50]; ll tot = 1; ll _max(ll a, ll b){ return a>b?a:b; } void solve() { ll ans = 1; memset(head, -1, sizeof(head)); for(ll i = k; i <= len; i++){ ll num = hash_[i]-hash_[i-k]*pp[k]; ll num2 = num%mm; int f = head[num2]; int sign = 0; while(f != -1) { if (arr[f].x == num) { arr[f].cnt++; ans = _max(ans, arr[f].cnt); sign = 1; break; } f = arr[f].next; } if (sign) continue; arr[tot].x = num; arr[tot].cnt = 1; arr[tot].next = head[num2]; head[num2] = tot++; } printf("%lld ", ans); } int main() { scanf("%s", s+1); len = strlen(s+1); scanf("%llu", &k); init_hash(); solve(); return 0; }