题目大意:
就是现在给出一个长度不超过100100的只包含小写字母的字符串, 求问这个串有多少个字串没有循环节, 如abab有循环节ab, 而aba, a, abc没有
大致思路:
好久没写题解了...补一个历史遗留的坑...
首先这题要先枚举循环节长度, 然后找到所有的可以以这个长度为循环节的串
根据每次枚举的循环节长度将串分成多个长度为L的组, 然后用后缀数组找出连续的一整段子串, 这个子串的任意长度是L的n(n >= 2)倍长度的子串都是不合法串, 那么因为这样枚举的时候会出现不同的L枚举到同一个串的情况, 这里用莫比乌斯函数来处理一下就好了
例如对于某个子串 S = abababababab当枚举循环节长度为2的时候S作为6个循环节计算一次, 枚举循环节长度为2, 3的时候也会算到, 循环节个数依次是3, 2, 那么答案就根据莫比乌斯函数来累加,, 为mu[6] + mu[3] + mu[2] = -1也就只减去了一次, 同理S = aaaaaaaaaaaa在计算的时候循环节个数在枚举循环节长度为1, 2, 3, 4, 6的时候算到, 计数就是mu[12] + mu[6] + mu[4] + mu[3] + mu[2] = -1, 想到莫比乌斯函数就是一个简单的计数了..
代码如下:
Result : Accepted Memory : 22052 KB Time : 1263 ms
/* * Author: Gatevin * Created Time: 2015/9/26 18:55:50 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define ws Ws #define rank rrrank const int maxn = 100200 + 1;//Mobius筛到maxn - 1 int prime[maxn], vis[maxn], mu[maxn]; int tot; void Mobius(){ mu[1] = 1; for( int i = 2 ; i < maxn ; i ++ ){ if( !vis[i] ) prime[++tot] = i, mu[i] = -1; for( int j = 1 ; j <= tot && i * prime[j] < maxn ; j ++ ){ vis[i*prime[j]] = 1; if( i % prime[j] == 0 ){ mu[i*prime[j]] = 0; break; } else mu[i*prime[j]] = - mu[i]; } } return; } int wa[maxn], wb[maxn], wv[maxn], ws[maxn]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void da(int *r, int *sa, int n, int m) { int *x = wa, *y = wb, *t, i, j, p; for(i = 0; i < m; i++) ws[i] = 0; for(i = 0; i < n; i++) ws[x[i] = r[i]]++; for(i = 1; i < m; i++) ws[i] += ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; for(j = 1, p = 1; p < n; j <<= 1, m = p) { for(p = 0, i = n - j; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j; for(i = 0; i < n; i++) wv[i] = x[y[i]]; for(i = 0; i < m; i++) ws[i] = 0; for(i = 0; i < n; i++) ws[wv[i]]++; for(i = 1; i < m; i++) ws[i] += ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } return; } int rank[maxn], height[maxn]; void calheight(int *r, int *sa, int n) { int i, j, k = 0; for(i = 1; i <= n; i++) rank[sa[i]] = i; for(i = 0; i < n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++); return; } int rank2[maxn], height2[maxn]; void calheight2(int *r, int *sa, int n) { int i, j, k = 0; for(i = 1; i <= n; i++) rank2[sa[i]] = i; for(i = 0; i < n; height2[rank2[i++]] = k) for(k ? k-- : 0, j = sa[rank2[i] - 1]; r[i + k] == r[j + k]; k++); return; } int dp[maxn][20]; void initRMQ(int n) { for(int i = 1; i <= n; i++) dp[i][0] = height[i]; for(int j = 1; (1 << j) <= n; j++) for(int i = 1; i + (1 << j) - 1 <= n; i++) dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); return; } int dp2[maxn][20]; void initRMQ2(int n) { for(int i = 1; i <= n; i++) dp2[i][0] = height2[i]; for(int j = 1; (1 << j) <= n; j++) for(int i = 1; i + (1 << j) - 1 <= n; i++) dp2[i][j] = min(dp2[i][j - 1], dp2[i + (1 << (j - 1))][j - 1]); return; } int askRMQ(int a, int b) { int ra = rank[a], rb = rank[b]; if(ra > rb) swap(ra, rb); int k = 0; while((1 << (k + 1)) <= rb - ra) k++; return min(dp[ra + 1][k], dp[rb - (1 << k) + 1][k]); } int len; int askRMQ2(int a, int b) { a = len - a - 1; b = len - b - 1; int ra = rank2[a], rb = rank2[b]; if(ra > rb) swap(ra, rb); int k = 0; while((1 << (k + 1)) <= rb - ra) k++; return min(dp2[ra + 1][k], dp2[rb - (1 << k) + 1][k]); } char in[maxn]; int s[maxn], sa[maxn]; int main() { Mobius(); int T; scanf("%d", &T); while(T--) { scanf("%s", in); len = strlen(in); for(int i = 0; i < len; i++) s[i] = in[i] - 'a' + 1; s[len] = 0; da(s, sa, len + 1, 28); calheight(s, sa, len); initRMQ(len); for(int i = 0; i < len; i++) s[i] = in[len - 1 - i] - 'a' + 1; s[len] = 0; da(s, sa, len + 1, 28); calheight2(s, sa, len); initRMQ2(len); lint ans = 0; for(int L = 1; L <= (len >> 1); L++)//枚举循环节长度 { int n = len; int blocks = n / L + (n % L != 0); int now = 1; while(now < blocks) { if(now + 1 < blocks) { int len2 = askRMQ2((now + 1)*L - 1, now*L - 1);//用后缀数组向前和向后找到区间[L, R]这个串中任意长度是L的2以上倍数的子串都不合法 int len1 = askRMQ(now*L, (now + 1)*L); int totlen = L + len2 + len1; int cnt = totlen / L; for(int i = 2; i <= cnt; i++) { ans += mu[i]*(totlen - L*i + 1); } //now = now + (len1 / L) + (len1 % L != 0) + 1;注意不要跳掉len1 % L != 0的时候中间那个没用完的段 //printf("find1 L = %d, ans = %I64d, totlen = %d, now = %d\n", L, ans, totlen, now); now = now + (len1 / L) + 1; } else { if(n % L != 0) break;//注意末尾...刚开始这里没注意到...对于abababababab一个个数终于发现了这个错误 int totlen = (len - now*L) + askRMQ2(len - 1, now*L - 1); int cnt = totlen / L; //int num = totlen % L + 1; for(int i = 2; i <= cnt; i++) ans += mu[i]*(totlen - L*i + 1); //printf("find2 L = %d, ans = %I64d, totlen = %d, now = %d\n", L, ans, totlen, now); now = blocks; } } } printf("%I64d\n", (lint)len*(len + 1LL) / 2LL + ans); } return 0; } /* 5 aaccaa abcabc aaaaaa abababababab 18 20 6 53 */