题目大意:
就是现在给出一个长度不超过20W的字符串S, 对于这个字符串求其所有前缀在串中出现次数的和, 结果对10007取模
大致思路:
很容易想到后缀数组, 找到sa[i] = 0的那个就是串S, 那么找出这个位置向两边能扩展到的长度即可, 就是利用一下height数组就行了, 没什么难度= =
还是不清楚的话看代码细节吧
另外也可以用KMP + DP来做
首先next[i]表示在第i个字符处适配了应该匹配第几个字符, 那么用dp[i]表示以第i个字符作为结尾的匹配的前缀的个数, 就可以有
dp[i] = dp[next[i]] + 1, 所有的dp的和就是最终的结果了
代码如下:
后缀数组的做法:
Result : Accepted Memory : 9608 KB Time : 452 ms
/* * Author: Gatevin * Created Time: 2015/5/4 16:03:18 * File Name: Rin_Tohsaka.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 foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) #define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl #define maxn 200010 #define rank rankrankrank 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; } char in[maxn]; int s[maxn], sa[maxn]; const int mod = 10007; int L[maxn], R[maxn]; int main() { int T; scanf("%d", &T); while(T--) { int len; scanf("%d", &len); scanf("%s", 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); int pos = -1; for(int i = 0; i <= len; i++) if(sa[i] == 0) { pos = i;//找到pos的位置 break; } int now = len; //[L[i], R[i]]是和sa[pos]公共前缀 >= i的连续一段 for(int i = pos + 1; i <= len; i++) { if(height[i] < now) { for(int j = height[i] + 1; j <= now; j++) R[j] = i - 1; now = height[i]; } } now = len; for(int i = pos; i >= 0; i--) { if(height[i] < now) { for(int j = height[i] + 1; j <= now; j++) L[j] = i; now = height[i]; } } int ans = 0; for(int i = 1; i <= len; i++) ans = (ans + R[i] - L[i] + 1) % mod; printf("%d\n", ans); } return 0; }
KMP + DP的做法:
Result : Accepted Memory : 3332 KB Time : 46 ms
/* * Author: Gatevin * Created Time: 2015/5/4 18:31:56 * File Name: Rin_Tohsaka.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 foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) #define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl #define maxn 200010 #define next nextnext int next[maxn]; void KMP(char *s, int n) { memset(next, 0, sizeof(next)); for(int i = 1; i < n; i++) { int j = i; while(j > 0) { j = next[j]; if(s[i] == s[j]) { next[i + 1] = j + 1; break; } } } //next[i]表示在第i个字符处失配时应该转到第几个字符 } char s[maxn]; int n; int dp[maxn];//用dp[i]表示以第i个字符结尾的匹配前缀有多少个 const int mod = 10007; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); scanf("%s", s); KMP(s, n); dp[0] = 0; int ans = 0; for(int i = 1; i <= n; i++) { dp[i] = dp[next[i]] + 1; if(dp[i] >= mod) dp[i] -= mod; ans += dp[i]; if(ans >= mod) ans -= mod; } printf("%d\n", ans); } return 0; }