您的位置:首页 > Web前端 > JavaScript

求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix

2015-07-15 12:11 519 查看

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 4164 Solved: 1277
[Submit][Status][Discuss]

Description

火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

Input

第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操作有3种,如下所示: 1、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x = 0,则在字符串开头插入。限制:x不超过当前字符串长度。

Output

对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

Sample Input

madamimadam

7

Q 1 7

Q 4 8

Q 10 11

R 3 a

Q 1 7

I 10 a

Q 2 11

Sample Output

5

1

0

2

1

HINT

数据规模:

对于100%的数据,满足:

1、 所有字符串自始至终都只有小写字母构成。

2、 M <= 150,000

3、 字符串长度L自始至终都满足L <= 100,000

4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

Source

弃疗!!!!

拿hzwer学长的程序拍了一天啊!!!屁都没拍出来!!!!!

hash函数改了千百遍,照样在一个奇葩点跪。。。、

还请大神们帮我啃啃好嘛。。。。。。。。。。。。

我的:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define CH for(int d=0;d<2;d++) if(ch[d])
#define lson x->ch[0],L,M-1
#define rson x->ch[1],M+1,R
using namespace std;
const int maxn=200000+10,inf=-1u>>1,hash=27;int n,Q;
const long long mod=98799321;
long long p[maxn];
struct node{
node*ch[2],*fa;int siz;long long h,x;
void update(){
siz=1;
if(ch[0]&&ch[1]){
siz+=ch[0]->siz+ch[1]->siz;
h=ch[0]->h+(long long)x*p[ch[0]->siz]%mod+(long long)ch[1]->h*p[ch[0]->siz+1]%mod;
}else if(ch[0]){
siz+=ch[0]->siz;
h=ch[0]->h+(long long)x*p[ch[0]->siz]%mod;
}else if(ch[1]){
siz+=ch[1]->siz;
h=x+(long long)ch[1]->h*p[1]%mod;
}else{
h=x;
} h%=mod;return;
}
}Splay[maxn],*nodecnt=Splay,*root;
int parent(node*x,node*&y){return (y=x->fa)?y->ch[1]==x?1:y->ch[0]==x?0:-1:-1;}
void rotate(node*x){
node*y,*z;int d1=parent(x,y),d2=parent(y,z);
if(y->ch[d1]=x->ch[d1^1]) y->ch[d1]->fa=y;
y->fa=x;x->fa=z;x->ch[d1^1]=y;
if(d2!=-1) z->ch[d2]=x;
y->update();return;
}
node*splay(node*x){
node*y,*z;int d1,d2;
while(true){
if((d1=parent(x,y))<0) break;
if((d2=parent(y,z))<0){rotate(x);break;}
if(d1==d2) rotate(y),rotate(x);
else rotate(x),rotate(x);
} x->update();return x;
}
char A[maxn];
void build(node*&x,int L,int R){
if(L>R)return;x=nodecnt++;
int M=L+R>>1;x->x=A[M]-'a';build(lson);build(rson);
if(x->ch[0]) x->ch[0]->fa=x;
if(x->ch[1]) x->ch[1]->fa=x;x->update();return;
}
node*find(node*x,int k){
int kth=x->ch[0]?x->ch[0]->siz+1:1;
if(k==kth)return x;
if(k<kth)return find(x->ch[0],k);return find(x->ch[1],k-kth);
}
node*findlast(node*x){
while(x->ch[1]) x=x->ch[1];return x;
}
void split(node*&x,node*&y,int a){
if(!a){y=x;x=NULL;return;}
x=splay(find(x,a));y=x->ch[1];x->ch[1]=NULL;
if(y)y->fa=NULL;x->update();return;
}
void split(node*&x,node*&y,node*&z,int a,int b){
split(x,z,b);split(x,y,a-1);return;
}
void join(node*&x,node*y){
if(!x){x=y;return;}if(!y)return;
x=splay(findlast(x));x->ch[1]=y;
if(y)y->fa=x;x->update();return;
}
void join(node*&x,node*y,node*z){
join(y,z);join(x,y);return;
}
void print(node*x){
if(!x)return;
print(x->ch[0]);
printf("%c",x->x+'a');
print(x->ch[1]);
return;
}
void insert(int k){
node*x,*y;split(root,x,k);
//puts("insert:");print(root);PAU;print(x);ENT;
scanf("%s",A);build(y,0,strlen(A)-1);
join(root,y,x);n++;
}
int query(int k1,int k2){
node*x,*y;split(root,x,y,k1,k2);int ans=splay(x)->h;join(root,x,y);return ans;
}
int solve(int k1,int k2){
if(k1>k2)swap(k1,k2);
if(k1==k2)return n-k1+1;
int L=0,R=min(n-k1,n-k2)+1,ans=-1,M;
//printf("L:%d R:%d erfen_L:%d erfen_R:%d\n",k1,k2,L,R);
while(L<R){
M=L+R>>1;
if(query(k1,k1+M)==query(k2,k2+M)) L=M+1;
else R=M;
//printf("ing:[%d %d]\n",L,R);
}
//printf("ans:%d\n",L);
return L;
}
void update(int k,int c){
node*x,*y;split(root,x,y,k,k);
/*puts("root!");print(root);
puts("x!");print(x);
puts("y!");print(y);*/
x->x=c;x->update();join(root,x,y);/*puts("zyq:");print(root);*/return;
}
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();}
while(isdigit(ch)) x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x;
int len=0,buf[15];while(x) buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return;
}
inline char readc(){
char ch=getchar();while(!isalpha(ch))ch=getchar();return ch;
}
void init(){
//freopen("std01.in","r",stdin);
//freopen("chxer.out","w",stdout);
scanf("%s",A);n=strlen(A);
p[0]=1;for(int i=1;i<=n;i++) p[i]=p[~-i]*hash%mod;
build(root,0,n-1);
//print(root);
return;
}
void work(){
int ql,qr;char tp;
Q=read();
while(Q--){
tp=readc();
if(tp=='Q'){
ql=read();qr=read();write(solve(ql,qr));ENT;
}
else if(tp=='R') ql=read(),update(ql,readc()-'a');
else insert(read());
//printf("printroot:");print(root);puts("");
}
return;
}
void print(){
write(-1);
return;
}
int main(){
init();work();print();return 0;
}

/*
asdfasdasd
1000
I 10 f
Q 4 11
*/


hzwer学长的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
#define mod 9875321
using namespace std;
int c[150005][2],fa[150005],id[150005];
int size[150005],v[150005],h[150005],p[150005];
int n,m,rt,sz;
char ch[150005];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void update(int k)
{
int l=c[k][0],r=c[k][1];
size[k]=size[l]+size[r]+1;
h[k]=h[l]+(ll)v[k]*p[size[l]]%mod+(ll)p[size[l]+1]*h[r]%mod;
h[k]%=mod;
}
void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)l=0;else l=1;r=l^1;
if(y==k)k=x;
else {if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
update(y);update(x);
}
void splay(int x,int &k)
{
while(x!=k)
{
int y=fa[x],z=fa[y];
if(y!=k)
{
if(c[y][0]==x^c[z][0]==y)rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int k,int rk)
{
int l=c[k][0],r=c[k][1];
if(size[l]+1==rk)return k;
else if(size[l]>=rk)return find(l,rk);
else return find(r,rk-size[l]-1);
}
void insert(int k,int val)
{
int x=find(rt,k+1),y=find(rt,k+2);
splay(x,rt);splay(y,c[x][1]);
int z=++sz;c[y][0]=z;fa[z]=y;v[z]=val;
update(z);update(y);update(x);
}
int query(int k,int val)
{
int x=find(rt,k),y=find(rt,k+val+1);
splay(x,rt);splay(y,c[x][1]);
int z=c[y][0];
return h[z];
}
int solve(int x,int y)
{
int l=1,r=min(sz-x,sz-y)-1,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(query(x,mid)==query(y,mid))l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
void build(int l,int r,int f)
{
if(l>r)return;
int now=id[l],last=id[f];
if(l==r)
{
v[now]=h[now]=ch[l]-'a'+1;
fa[now]=last;size[now]=1;
if(l<f)c[last][0]=now;
else c[last][1]=now;
return;
}
int mid=(l+r)>>1;now=id[mid];
build(l,mid-1,mid);build(mid+1,r,mid);
v[now]=ch[mid]-'a'+1;fa[now]=last;update(now);
if(mid<f)c[last][0]=now;
else c[last][1]=now;
}
int main()
{
//freopen("std01.in","r",stdin);
//freopen("hzwer.out","w",stdout);
scanf("%s",ch+2);
n=strlen(ch+2);
p[0]=1;for(int i=1;i<=150004;i++)p[i]=p[i-1]*27%mod;
for(int i=1;i<=n+2;i++)id[i]=i;
build(1,n+2,0);sz=n+2;rt=(n+3)>>1;
m=read();
int x,y;
char s[2],d[2];
for(int i=1;i<=m;i++)
{
scanf("%s",s+1);
x=read();
switch(s[1])
{
case 'Q':y=read();printf("%d\n",solve(x,y));break;
case 'R':
{
scanf("%s",d+1);x=find(rt,x+1);splay(x,rt);
v[rt]=d[1]-'a'+1;update(rt);break;
}
case 'I':scanf("%s",d+1);insert(x,d[1]-'a'+1);break;
}
}
return 0;
}
/*
abaabbbbbbbbbaaaabba
1000
Q 1 9
Q 6 6
Q 2 9
Q 7 13
Q 14 18
Q 18 18
Q 5 6
*/


生成数据:

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cstring>
#include <windows.h>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();}
while(isdigit(ch)) x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x;
int len=0,buf[15];while(x) buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return;
}
const int maxl=99999;

char in[15] = "std00.in";
char out[15] = "std00.out";
const int L_file = 1;
const int R_file = 2;

void _init()   //每次循环的数值清空
{
//此处清零

return ;
}

void _in()
{
in[8] = '\0';
out[9] = '\0';
for(int __i = L_file; __i <= R_file; __i ++)
{
in[3] = __i / 10 + '0', in[4] = __i % 10 + '0';
if(freopen(in, "w", stdout) == NULL) system("in失败");

//此处开始打印测试数据
int Q=100000;
for(int i=1;i<=20;i++){
putchar(rand()%3+'a');
}
int len=20;
puts("");
write(Q);ENT;int tp;
for(int i=1;i<=Q;i++){
tp=rand()%2;
if(!tp){
putchar('Q');PAU;int t,t2;
write(t=rand()%(len-1)+1);PAU;
while(t>(t2=rand()%len+1));
write(t2);ENT;
}
else if(tp==1){
putchar('I');PAU;write(rand()%len);PAU;putchar(rand()%2+'a');ENT;len++;
}
else {
putchar('R');PAU;write(rand()%len+1);PAU;putchar(rand()%2+'a');ENT;
}
}

system("已处理完一项输入数据");
}
return ;
}

void _out()
{
int n, m;
for(int __i = L_file; __i <= R_file; __i ++)
{
_init();
in[3] = __i / 10 + '0', in[4] = __i % 10 + '0';
if(freopen(in, "r", stdin) == NULL) system("in2失败");

out[3] = __i / 10 + '0', out[4] = __i % 10 + '0';
if(freopen(out, "w", stdout) == NULL) system("out失败");

//此处放代码

system("已处理完一项输出数据");
}
return ;
}

int main()
{
srand((unsigned)time(0));

_in();
//_out();

fclose(stdin);
fclose(stdout);

return 0;
}


对拍程序:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<fstream>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
ifstream fin1 ("CHXER.out");
ifstream fin2 ("HZWER.out");
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
char a1,a2;int len=0;
bool check(){
fin1>>a1;
fin2>>a2;
//printf("%c %c\n",a1,a2);
if(a1==a2)return true;
else return false;
}
void init(){
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
while(check())len++;
printf("%c %c %d\n",a1,a2,len);
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: