1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc=new FastScanner(); PrintWriter pw=new PrintWriter(System.out); SA sa=new SA(); while(sc.hasNext()){ String s=sc.next(); sa.getSA(s); sa.getHeight(); int n=sc.nextInt(); long left=0; long right=0; int prel=sc.nextInt(); int prer=sc.nextInt(); left+=prer-prel+1; right+=3+prer-prel; sa.rmqinit(); for(int o=2;o<=n;o++){ int l=sc.nextInt(); int r=sc.nextInt(); left+=r-l+1; int k=Math.min(sa.queryLCP(prel, l), Math.min(prer-prel, r-l)); right+=r-l+2+String.valueOf(k).length()-k; prel=l; prer=r; } pw.println(left+" "+right); pw.flush(); } } } class SA{
int maxn=100050; int[]sa,wa,wb,ws,wv; int[]rank; int[]height; int[]r; int[][]dp; int n; int m; SA(){ sa=new int[maxn]; wa=new int[maxn]; wb=new int[maxn]; ws=new int[maxn]; wv=new int[maxn]; height=new int[maxn]; rank=new int[maxn]; r=new int[maxn]; dp=new int[maxn][20]; } boolean cmp(int[]f,int x,int y,int w){ return f[x] == f[y] && f[x + w] == f[y + w]; } void getSA(String s){ n=s.length(); for(int i=0;i<s.length();i++){ r[i]=s.charAt(i); } r[n]=0; da(r,sa,s.length()+1,128); } void getHeight(){ calheight(r,sa,n); } void da(int[] r, int sa[], int n, int m){ int i, j, p; int[]x = wa; int[]y = wb; int[]temp; 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 (temp=x,x=y,y=temp, 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++; } } 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){ if(k>0)k--; for (j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++); } } void rmqinit(){ 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]=Math.min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int queryLCP(int i,int j){ if(i==j) return n-i; if(rank[i]<rank[j]) return q(rank[i]+1,rank[j]); else return q(rank[j]+1,rank[i]); } int q(int l,int r){ int k=(int) (Math.log(r-l+1)/Math.log(2)); return Math.min(dp[l][k],dp[r-(1<<k)+1][k]); } }
|