题目大意:
就是按照题目给出的压缩方式计算初始的数据长度和压缩后的数据长度
大致思路:
其实就是相当于对于N次询问要查询后缀l1, 后后缀l2的最长公共前缀, 注意l1 == l2的时候直接判就行了
然后就是之一公共长度的位数也会影响结果, 例如10这个要占两个空间
代码如下:
Result : Accepted Memory : 12636 KB Time : 904 ms
/* * Author: Gatevin * Created Time: 2015/5/5 9:46:59 * 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 rank rankrankrank #define maxn 100010 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 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 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]); } char in[maxn]; int s[maxn], sa[maxn]; int main() { while(scanf("%s", in) != EOF) { int n = strlen(in); for(int i = 0; i < n; i++) s[i] = in[i] - 'a' + 1; s[n] = 0; da(s, sa, n + 1, 28); calheight(s, sa, n); initRMQ(n); int Q; scanf("%d", &Q); lint res1 = 0; lint res2 = 0; int l0 = -1, r0 = -1; int l, r; while(Q--) { scanf("%d %d", &l, &r); res2 += 2;//空格和回车 res1 += (r - l) + 1; int len; if(l0 == -1) len = 0; else if(l0 == l) len = min(r, r0) - l0; else len = askRMQ(l0, l); int maxlen1 = r0 - l0; int maxlen2 = r - l; if(len > maxlen1) len = maxlen1; if(len > maxlen2) len = maxlen2; res2 += maxlen2 - len; l0 = l, r0 = r; int cnt = 1; while(len / 10) len /= 10, cnt++; res2 += cnt;//公共前缀长度十进制的位数 } printf("%I64d %I64d\n", res1, res2); } return 0; }