frcode 2 0 6 0 6 unitedstatesofamerica 3 0 6 0 12 0 21 myxophytamyxopodnabnabbednabbingnabit 6 0 9 9 16 16 19 19 25 25 32 32 37
14 12 42 31 43 40
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100010; 18 int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn]; 19 bool cmp(int *r,int i,int j,int k){ 20 return r[i] == r[j] && r[i+k] == r[j+k]; 21 } 22 void da(int *r,int *sa,int n,int m){ 23 int i,k,p,*x = rk,*y = wb; 24 for(i = 0; i < m; ++i) wd[i] = 0; 25 for(i = 0; i < n; ++i) wd[x[i] = r[i]]++; 26 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 27 for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i; 28 29 for(p = k = 1; p < n; k <<= 1,m = p){ 30 for(p = 0,i = n-k; i < n; ++i) y[p++] = i; 31 for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k; 32 for(i = 0; i < n; ++i) wv[i] = x[y[i]]; 33 34 for(i = 0; i < m; ++i) wd[i] = 0; 35 for(i = 0; i < n; ++i) wd[wv[i]]++; 36 for(i = 1; i < m; ++i) wd[i] += wd[i-1]; 37 for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i]; 38 39 swap(x,y); 40 x[sa[0]] = 0; 41 for(p = i = 1; i < n; ++i) 42 x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++; 43 } 44 } 45 void calcp(int *r,int *sa,int n){ 46 for(int i = 1; i <= n; ++i) rk[sa[i]] = i; 47 int h = 0; 48 for(int i = 0; i < n; ++i){ 49 if(h > 0) h--; 50 for(int j = sa[rk[i]-1]; i+h < n && j+h < n; h++) 51 if(r[i+h] != r[j+h]) break; 52 lcp[rk[i]] = h; 53 } 54 } 55 int st[maxn][20]; 56 void init(int n){ 57 memset(st,0,sizeof(st)); 58 for(int i = 1; i <= n; ++i) st[i][0] = lcp[i]; 59 for(int i = 1; 1<<i <= n; ++i){ 60 for(int j = 1; j+(1<<i) <= n+1; ++j) 61 st[j][i] = min(st[j][i-1],st[j+(1<<(i-1))][i-1]); 62 } 63 } 64 int query(int s,int t){ 65 s = rk[s]; 66 t = rk[t]; 67 if(s > t) swap(s,t); 68 s++; 69 int k = log2(t - s + 1); 70 return min(st[s][k],st[t-(1<<k)+1][k]); 71 } 72 int r[maxn],sa[maxn]; 73 char str[maxn]; 74 int mb(int x){ 75 int ans = 0; 76 if(x == 0) return 1; 77 while(x){ 78 x /= 10; 79 ++ans; 80 } 81 return ans; 82 } 83 int main() { 84 int hn,x,y; 85 while(~scanf("%s",str)){ 86 int len = strlen(str); 87 for(int i = 0; str[i]; ++i) 88 r[i] = str[i]; 89 r[len] = 0; 90 da(r,sa,len+1,128); 91 calcp(r,sa,len); 92 init(len); 93 LL ans = 0,ans2 = 0; 94 int px,py; 95 scanf("%d",&hn); 96 for(int i = 0; i < hn; ++i){ 97 scanf("%d %d",&x,&y); 98 ans += y - x + 1; 99 if(i == 0) ans2 += y - x + 3; 100 else{ 101 int mlen = min(y-x,py-px); 102 if(px == x){ 103 if(mlen == y - x) ans2 += mb(y-x) + 2; 104 else ans2 += mb(mlen) + 2 + y - x - mlen; 105 }else{ 106 int com = query(px,x); 107 if(com >= mlen){ 108 com = mlen; 109 if(com == y - x) ans2 += mb(com) + 2; 110 else ans2 += mb(com) + y - x - com + 2; 111 }else if(com) ans2 += mb(com) + 2 + y - x - com; 112 else ans2 += y - x + 3; 113 } 114 } 115 px = x; 116 py = y; 117 } 118 cout<<ans<<" "<<ans2<<endl; 119 } 120 return 0; 121 }