标签:clu emc ast clipboard hover mes ber const icpc
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; char str[maxn]; int hou[maxn *2][30],link[maxn *2], num[maxn *2]; ll End[maxn *2]; int stra[maxn], strb[maxn *2],Size, last, len; void add(int c){ int p = last,np = ++Size; last = np,End[np] = 1; num[np] = num[p] + 1; while(!hou[p][c] && p) hou[p][c] = np,p = link[p]; if(p == 0) link[np] = 1; else{ int q = hou[p][c]; if(num[p] + 1 == num[q]) link[np] = q; else{ int temp = ++Size; memcpy(hou[temp], hou[q], sizeof(hou[q])); num[temp] = num[p] + 1; link[temp] = link[q]; link[q] = link[np] = temp; while(hou[p][c] == q && p) hou[p][c] = temp, p = link[p]; } } } void build(){ memset(hou, 0, sizeof(hou)); memset(End, 0, sizeof(End)); memset(stra, 0, sizeof(stra)); memset(strb, 0, sizeof(strb)); Size = last = 1; for(register int i = 0; i < len; ++i) add(str[i] - ‘A‘); for(register int i = 1; i <= Size; ++i) stra[num[i]]++; for(register int i = 1; i <= len; ++i) stra[i] += stra[i - 1]; for(register int i = 1; i <= Size; ++i) strb[stra[num[i]]--] = i; for(register int i = Size; i > 1; --i){ int e = strb[i]; End[link[e]] += End[e]; } } void solve(){ int A, B; scanf("%d %d", &A, &B); len = strlen(str); build(); ll ans = 0; for(register int i = 1; i <= Size; ++i) if(End[i] >= A && End[i] <= B) ans += num[i] - num[link[i]]; printf("%lld\n", ans); } int main(){ while(~scanf("%s", str)) solve(); return 0; }
AAA 2 3
ABAB 2 2
2
3
标签:clu emc ast clipboard hover mes ber const icpc
原文地址:https://www.cnblogs.com/lengsong/p/11331069.html