数据结构·后缀数组
2016-08-04 19:17
197 查看
相关习题:
Uva 10829 L-Gap Sustrings
代码:
code:
Uva 10829 L-Gap Sustrings
代码:
#include <algorithm> #include <cstdio> #include <cstring> #include <string> #include <queue> #include <map> #include <set> #include <ctime> #include <cmath> #include <iostream> #include <assert.h> #define PI acos(-1.) #pragma comment(linker, "/STACK:102400000,102400000") #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define mp make_pair #define st first #define nd second #define keyn (root->ch[1]->ch[0]) #define lson (u << 1) #define rson (u << 1 | 1) #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define type(x) __typeof(x.begin()) #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++) #define FOR(i, s, t) for(int i = (s); i <= (t); i++) #define ROF(i, t, s) for(int i = (t); i >= (s); i--) #define dbg(x) cout << x << endl #define dbg2(x, y) cout << x << " " << y << endl #define clr(x, i) memset(x, (i), sizeof(x)) #define maximize(x, y) x = max((x), (y)) #define minimize(x, y) x = min((x), (y)) //using namespace std; typedef long long ll; const int int_inf = 0x3f3f3f3f; const ll ll_inf = 0x3f3f3f3f3f3f3f3f; const int INT_INF = (int)((1ll << 31) - 1); const double double_inf = 1e30; const double eps = 1e-14; typedef unsigned long long ul; inline int readint(){ int x; scanf("%d", &x); return x; } inline int readstr(char *s){ scanf("%s", s); return strlen(s); } //Here goes 2d geometry templates struct Point{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; typedef Point Vector; Vector operator + (Vector A, Vector B){ return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Point A, Point B){ return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p){ return Vector(A.x * p, A.y * p); } Vector operator / (Vector A, double p){ return Vector(A.x / p, A.y / p); } bool operator < (const Point& a, const Point& b){ return a.x < b.x || (a.x == b.x && a.y < b.y); } int dcmp(double x){ if(abs(x) < eps) return 0; return x < 0 ? -1 : 1; } bool operator == (const Point& a, const Point& b){ return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Vector A, Vector B){ return A.x * B.x + A.y * B.y; } double Len(Vector A){ return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B){ return acos(Dot(A, B) / Len(A) / Len(B)); } double Cross(Vector A, Vector B){ return A.x * B.y - A.y * B.x; } double Area2(Point A, Point B, Point C){ return Cross(B - A, C - A); } Vector Rotate(Vector A, double rad){ //rotate counterclockwise return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } Vector Normal(Vector A){ double L = Len(A); return Vector(-A.y / L, A.x / L); } void Normallize(Vector &A){ double L = Len(A); A.x /= L, A.y /= L; } Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){ Vector u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } double DistanceToLine(Point P, Point A, Point B){ Vector v1 = B - A, v2 = P - A; return abs(Cross(v1, v2)) / Len(v1); } double DistanceToSegment(Point P, Point A, Point B){ if(A == B) return Len(P - A); Vector v1 = B - A, v2 = P - A, v3 = P - B; if(dcmp(Dot(v1, v2)) < 0) return Len(v2); else if(dcmp(Dot(v1, v3)) > 0) return Len(v3); else return abs(Cross(v1, v2)) / Len(v1); } Point GetLineProjection(Point P, Point A, Point B){ Vector v = B - A; return A + v * (Dot(v, P - A) / Dot(v, v)); } bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){ //Line1:(a1, a2) Line2:(b1,b2) double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } bool OnSegment(Point p, Point a1, Point a2){ return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0; } Vector GetBisector(Vector v, Vector w){ Normallize(v), Normallize(w); return Vector((v.x + w.x) / 2, (v.y + w.y) / 2); } bool OnLine(Point p, Point a1, Point a2){ Vector v1 = p - a1, v2 = a2 - a1; double tem = Cross(v1, v2); return dcmp(tem) == 0; } struct Line{ Point p; Vector v; Point point(double t){ return Point(p.x + t * v.x, p.y + t * v.y); } Line(Point p, Vector v) : p(p), v(v) {} }; struct Circle{ Point c; double r; Circle(Point c, double r) : c(c), r(r) {} Circle(int x, int y, int _r){ c = Point(x, y); r = _r; } Point point(double a){ return Point(c.x + cos(a) * r, c.y + sin(a) * r); } }; int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, std :: vector<Point>& sol){ double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y; double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r; double delta = f * f - 4 * e * g; if(dcmp(delta) < 0) return 0; if(dcmp(delta) == 0){ t1 = t2 = -f / (2 * e); sol.pb(L.point(t1)); return 1; } t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1)); t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2)); return 2; } double angle(Vector v){ return atan2(v.y, v.x); //(-pi, pi] } int GetCircleCircleIntersection(Circle C1, Circle C2, std :: vector<Point>& sol){ double d = Len(C1.c - C2.c); if(dcmp(d) == 0){ if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates return 0; //two circles share identical center } if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away double a = angle(C2.c - C1.c); // angle of vector(C1, C2) double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d)); Point p1 = C1.point(a - da), p2 = C1.point(a + da); sol.pb(p1); if(p1 == p2) return 1; sol.pb(p2); return 2; } int GetPointCircleTangents(Point p, Circle C, Vector* v){ Vector u = C.c - p; double dist = Len(u); if(dist < C.r) return 0;//p is inside the circle, no tangents else if(dcmp(dist - C.r) == 0){ // p is on the circles, one tangent only v[0] = Rotate(u, PI / 2); return 1; }else{ double ang = asin(C.r / dist); v[0] = Rotate(u, -ang); v[1] = Rotate(u, +ang); return 2; } } int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){ //a[i] store point of tangency on Circle A of tangent i //b[i] store point of tangency on Circle B of tangent i //six conditions is in consideration int cnt = 0; if(A.r < B.r) { std :: swap(A, B); std :: swap(a, b); } int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y); int rdiff = A.r - B.r; int rsum = A.r + B.r; if(d2 < rdiff * rdiff) return 0; // one circle is inside the other double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x); if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates if(d2 == rdiff * rdiff){ // internal tangency a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++; return 1; } double ang = acos((A.r - B.r) / sqrt(d2)); a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang); a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang); if(d2 == rsum * rsum){ //one internal tangent a[cnt] = A.point(base); b[cnt++] = B.point(base + PI); }else if(d2 > rsum * rsum){ //two internal tangents double ang = acos((A.r + B.r) / sqrt(d2)); a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI); a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI); } return cnt; } Point ReadPoint(){ double x, y; scanf("%lf%lf", &x, &y); return Point(x, y); } Circle ReadCircle(){ double x, y, r; scanf("%lf%lf%lf", &x, &y, &r); return Circle(x, y, r); } //Here goes 3d geometry templates struct Point3{ double x, y, z; Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {} }; typedef Point3 Vector3; Vector3 operator + (Vector3 A, Vector3 B){ return Vector3(A.x + B.x, A.y + B.y, A.z + B.z); } Vector3 operator - (Vector3 A, Vector3 B){ return Vector3(A.x - B.x, A.y - B.y, A.z - B.z); } Vector3 operator * (Vector3 A, double p){ return Vector3(A.x * p, A.y * p, A.z * p); } Vector3 operator / (Vector3 A, double p){ return Vector3(A.x / p, A.y / p, A.z / p); } double Dot3(Vector3 A, Vector3 B){ return A.x * B.x + A.y * B.y + A.z * B.z; } double Len3(Vector3 A){ return sqrt(Dot3(A, A)); } double Angle3(Vector3 A, Vector3 B){ return acos(Dot3(A, B) / Len3(A) / Len3(B)); } double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){ return abs(Dot3(p - p0, n)); } Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){ return p - n * Dot3(p - p0, n); } Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){ Vector3 v = p2 - p1; double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1)); return p1 + v * t;//if t in range [0, 1], intersection on segment } Vector3 Cross(Vector3 A, Vector3 B){ return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x); } double Area3(Point3 A, Point3 B, Point3 C){ return Len3(Cross(B - A, C - A)); } class cmpt{ public: bool operator () (const int &x, const int &y) const{ return x > y; } }; int Rand(int x, int o){ //if o set, return [1, x], else return [0, x - 1] if(!x) return 0; int tem = (int)((double)rand() / RAND_MAX * x) % x; return o ? tem + 1 : tem; } //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// void data_gen(){ srand(time(0)); freopen("in.txt", "w", stdout); int times = 100; printf("%d\n", times); while(times--){ int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1); int b = Rand(r, 1), d = Rand(r, 1); int m = Rand(100, 1), n = Rand(m, 1); printf("%d %d %d %d %d %d %d\n", n, m, a, b, c, d, r); } } struct cmpx{ bool operator () (int x, int y) { return x > y; } }; int debug = 1; int dx[] = {0, 0, 1, 1}; int dy[] = {1, 0, 0, 1}; //------------------------------------------------------------------------- const int maxn = 5e4 + 10; char str[maxn]; int s[maxn << 1]; int g, n, len; int sa[maxn << 1], t[maxn << 1], t2[maxn << 1], c[maxn << 1]; int rank[maxn << 1], height[maxn << 1]; int mini[20][maxn << 1]; void build_sa(int m){ int i, *x = t, *y = t2; FOR(i, 0, m - 1) c[i] = 0; FOR(i, 0, n - 1) c[x[i] = s[i]]++; FOR(i, 1, m - 1) c[i] += c[i - 1]; ROF(i, n - 1, 0) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1){ int p = 0; FOR(i, n - k, n - 1) y[p++] = i; FOR(i, 0, n - 1) if(sa[i] >= k) y[p++] = sa[i] - k; FOR(i, 0, m - 1) c[i] = 0; FOR(i, 0, n - 1) c[x[y[i]]]++; FOR(i, 0, m - 1) c[i] += c[i - 1]; ROF(i, n - 1, 0) sa[--c[x[y[i]]]] = y[i]; std :: swap(x, y); p = 1, x[sa[0]] = 0; FOR(i, 1, n - 1) x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++; if(p >= n) break; m = p; } } void getHeight(){ int i, j, k = 0; FOR(i, 0, n - 1) rank[sa[i]] = i; FOR(i, 0, n - 1){ if(k) k--; int j = sa[rank[i] - 1]; while(s[i + k] == s[j + k]) k++; height[rank[i]] = k; } } void init_rmq(){ FOR(i, 0, n - 1) mini[0][i] = height[i]; for(int j = 1; (1 << j) <= n; j++){ int len = 1 << j; FOR(i, 0, n - 1){ if(i + len > n) break; mini[j][i] = min(mini[j - 1][i], mini[j - 1][i + (len >> 1)]); } } } int lcp(int L, int R){ int l = rank[L], r = rank[R]; if(l > r) std :: swap(l, r); ++l, ++r; int len = r - l; int i = (int)log2(len + .5); return min(mini[i][l], mini[i][r - (1 << i)]); } ll solve(){ n = 0; FOR(i, 0, len - 1) s[n++] = str[i] - 'a' + 1; s[n++] = 0; ROF(i, len - 1, 0) s[n++] = str[i] - 'a' + 1; s = 27; build_sa(28); getHeight(); init_rmq(); ll ans = 0; FOR(d, 1, len / 2){ for(int i = 0; i < len; i += d){ int j = i + g + d; if(j >= len) break; int cnt = 0; cnt += min(lcp(i, j), d); cnt += min(lcp(n - 1 - i, n - 1 - j), d); ans += max(0, cnt - d); } } return ans; } //------------------------------------------------------------------------- int main(){ //data_gen(); return 0; //C(); return 0; debug = 0; /////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(debug) freopen("in.txt", "r", stdin); //freopen("in.txt", "w", stdout); int T = readint(), kase = 0; while(T--){ scanf("%d", &g); len = readstr(str); ll ans = solve(); printf("Case %d: %lld\n", ++kase, ans); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// return 0; }
code:
相关文章推荐
- 求最长重复子串(后缀数组)
- HDU 2328 Corporate Identity(后缀数组-求多个串的最长共同子串)
- HDU 4552 怪盗基德的挑战书 (KMP + DP) 或 后缀数组
- Hiho 122 后缀数组三·重复旋律3(多个串的最长公共重复子串)
- 【BZOJ 2946】【POI 2000】公共串【后缀数组】【裸】
- hihoCoder 1415 后缀数组三·重复旋律3(最长公共子串)
- [贪心+后缀数组] poj 3623 Best Cow Line, Gold
- POJ 3693 重复次数最多的子串是什么?【后缀数组】
- 用后缀数组 求一个字符串的最长重复字串
- 【HDU 4898】 The Revenge of the Princess’ Knight (后缀数组+二分+贪心+...)
- 【UOJ 35】 后缀排序|后缀数组 *3
- 后缀数组(一堆干货)
- POJ 2406 Power Strings 【后缀数组||KMP】
- UVA 11475 Extend to Palindrome 后缀数组 LCP
- 数据结构之后缀数组
- poj 3294 求多于k个字符串的最长公共子串的个数-------后缀数组+二分答案
- 【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]
- 关于网上搜查得到的3DC3的基于字符串后缀数组的排序方法的怀疑
- 后缀数组,处理字符串问题经常会用到
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)