题目:http://acm.hdu.edu.cn/showproblem.php?pid=4691
题意:
分析:直接求LCP啊。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ULL;
const int maxn = 1e5+6;
const int seed = 131;
ULL H[maxn],X[maxn];
int Len;
char str[maxn];
void Init()
{
H[Len]=0;
for(int i=Len-1;i>=0;i--)
H[i]=H[i+1]*seed+str[i]-'a';
X[0]=1;
for(int i=1;i<Len;i++)
X[i]=X[i-1]*seed;
}
ULL GetHash(int i,int L)
{
return H[i]-H[i+L]*X[L];
}
int LCP(int a,int b,int lim)
{
int ret=0,down=1,mid,up=lim;
while(down<=up)
{
mid=(down+up)>>1;
if(GetHash(a,mid)==GetHash(b,mid))
{
down=mid+1;
if(ret<mid)
ret=mid;
}
else
up=mid-1;
}
return ret;
}
int GetLen(int x)
{
if(x==0)
return 1;
int ret=0;
while(x)
{
ret++;
x/=10;
}
return ret;
}
int main()
{
int i,j,Q,a,b,lim,pera,perb,f;
long long in,ou;
while(scanf("%s",str)!=EOF)
{
Len=strlen(str);
Init();
scanf("%d",&Q);
scanf("%d%d",&pera,&perb);
in=perb-pera+1;
ou=3+perb-pera;
Q--;
while(Q--)
{
scanf("%d%d",&a,&b);
in+=b-a+1;
lim=min(perb-pera,b-a);
f=LCP(pera,a,lim);
ou+=GetLen(f)+1+(b-a-f)+1;
pera=a;
perb=b;
}
printf("%I64d %I64d\n",in,ou);
}
return 0;
}