题目大意:
白书例题
给出一个整数m>=1和一个字符串(m <= 长度 <= 40000), 找出最长的至少重复出现了m次的子串起始点和长度,可以有重叠部分地出现
如果有多个子串满足条件输出出现位置最右的
大致思路:
首先很容易想到后缀数组的做法. 利用height数组分组, 很简单就不说了, 细节见代码
另外白书上说可以用hash, 所以用这个题试了一下hash
表示第一次用hash...然后就坑了有木有= =
刚开始我取得是H函数中x = 2的情况来写
这样可以用位运算..然后交了好多次之后发现取x = 2就是一个错误...
后来把白书中的x从123改成2发现也WA了, 177,233什么的都可以过= =.....于是第一次写hash就被概率性地挂掉了...
两种方法的代码如下:
后缀数组解法:
Result : Accepted Memory : ? KB Time : 193 ms
/* * Author: Gatevin * Created Time: 2015/2/11 19:04:46 * File Name: Mononobe_Mitsuki.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 maxn 42333 /* * Doubling Algorithm求后缀数组 */ 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 *= 2, 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 s[maxn], sa[maxn]; char in[maxn]; int pos[maxn]; int M; bool check(int mid, int n, int& start)//对于连续出现M - 1次以上height值>=mid的情况更新start的答案 { int cnt = 1; bool ret = false; int tmp = 0; for(int i = 1; i <= n; i++) if(height[i] >= mid) { tmp = max(tmp, sa[i]);//当前连续片中的最右串 cnt++; if(cnt >= M) { ret = true; start = max(start, tmp);//更新start位置 } } else cnt = 1, tmp = sa[i]; return ret; } int main() { while(scanf("%d", &M), M) { scanf("%s", in); int n = strlen(in); if(M == 1) { printf("%d 0\n", n); continue; } for(int i = 0; i < n; i++) s[i] = in[i] - 'a' + 1; s[n] = 0; da(s, sa, n + 1, 27); calheight(s, sa, n); int L = 1, R = n, len = 0, mid;//二分判断是否存在长度为mid的字符串出现了M次以上 while(L <= R) { mid = (L + R) >> 1; pos[mid] = 0; if(check(mid, n, pos[mid])) { len = mid; L = mid + 1; } else R = mid - 1; } if(len == 0) printf("none\n"); else printf("%d %d\n", len, pos[len]); } return 0; }
hash的做法:
Result : Accepted Memory : ? KB Time : 1319 ms
/* * Author: Gatevin * Created Time: 2015/2/11 19:46:52 * File Name: Mononobe_Mitsuki.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; typedef unsigned long long ulint; #define maxn 42333 const ulint x = 233; /* * 刚开始取得x = 2然后就各种悲剧= =... * 因为当时想的x = 2的时候用位运算可能快一些 * 同时也利用了unsigned long long的溢出取模 * 然后跪了好几下实在没救了= = * 真是醉了... */ int M; char in[maxn]; ulint hash[maxn]; int pos[maxn]; int id[maxn]; ulint xp[maxn]; ulint H[maxn]; bool cmp(int a, int b) { return hash[a] < hash[b]; } bool check(int mid, int n) { pos[mid] = 0; id[0] = 0; for(int i = 0; i + mid - 1 < n; i++)//就算每一段的hash值 id[i] = i, hash[i] = H[i] - H[i + mid]*xp[mid]; sort(id, id + n - mid + 1, cmp);//按hash值排序 int cnt = 1; bool ret = false; int tmp = id[0]; for(int i = 1; i <= n - mid; i++) if(hash[id[i]] == hash[id[i - 1]]) { cnt++; tmp = max(tmp, id[i]);//注意片段性的最大tmp中取最大的作为pos[mid] if(cnt >= M) pos[mid] = max(pos[mid], tmp), ret = true; } else cnt = 1, tmp = id[i]; return ret; } int main() { xp[0] = 1; for(int i = 1; i <= 40000; i++)//预处理x次幂项 xp[i] = xp[i - 1]*x; while(scanf("%d", &M), M) { scanf("%s", in); int n = strlen(in); if(M == 1) { printf("%d 0\n", n); continue; } H[n] = 0; for(int i = n - 1; i >= 0; i--)//预处理H[i] = s[i] + s[i + 1]*x + ... + s[n]*x^(n - i) H[i] = H[i + 1]*x + (in[i] - 'a'); int L = 1, R = n, mid, len = 0; while(L <= R) { mid = (L + R) >> 1; if(check(mid, n)) { len = mid; L = mid + 1; } else R = mid - 1; } if(len == 0) printf("none\n"); else printf("%d %d\n", len, pos[len]); } return 0; }