1 //BZOJ 1014
2 #include<vector>
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 #include<iostream>
7 #include<algorithm>
8 #define rep(i,n) for(int i=0;i<n;++i)
9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 #define pb push_back
12 using namespace std;
13 inline int getint(){
14 int v=0,sign=1; char ch=getchar();
15 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
16 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
17 return v*sign;
18 }
19 const int N=3e5+10,INF=~0u>>2;
20 typedef long long LL;
21 typedef unsigned long long u64;
22 /******************tamplate*********************/
23 int n,m;
24 char s[N];
25 int c[N][2],fa[N],size[N],tot,root;
26 u64 sum[N],v[N],mi[N];
27 #define L c[x][0]
28 #define R c[x][1]
29 void Push_up(int x){
30 size[x]=size[L]+size[R]+1;
31 sum[x]=sum[L]*mi[size[R]+1]+v[x]*mi[size[R]]+sum[R];
32 }
33 void Rotate(int x){
34 int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
35 c[z][c[z][1]==y]=x;
36 fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
37 c[y][l]=c[x][r]; c[x][r]=y;
38 Push_up(y);
39 }
40 void splay(int x,int s=0){
41 int y;
42 for(;fa[x]!=s;Rotate(x))
43 if (fa[y=fa[x]]!=s) Rotate(c[y][1]==x^c[fa[y]][1]==y?x:y);
44 Push_up(x);
45 if (!s) root=x;
46 }
47 void New_node(int &x,int f,int key){
48 x=++tot;
49 fa[x]=f;
50 v[x]=sum[x]=key; L=R=0;
51 size[x]=1;
52 }
53 void Build(int &x,int f,int l,int r){
54 if (l>r) return;
55 int m=l+r>>1;
56 New_node(x,f,s[m]);
57 Build(L,x,l,m-1);
58 Build(R,x,m+1,r);
59 Push_up(x);
60 }
61 int kth(int x,int k){
62 if (size[L]+1==k) return x;
63 else if (size[L]>=k) return kth(L,k);
64 else return kth(R,k-size[L]-1);
65 }
66 int query(int x1,int x2){
67 int l=1,r=min(size[root]-x1,size[root]-x2)-1,mid,ans=0;
68 //由于右侧有一个空白字符,所以要-1
69 u64 n1=0,n2=0;
70 while(l<=r){
71 mid=l+r>>1;
72 splay(kth(root,x1)); splay(kth(root,x1+mid+1),root);
73 n1=sum[c[c[root][1]][0]];
74 splay(kth(root,x2)); splay(kth(root,x2+mid+1),root);
75 n2=sum[c[c[root][1]][0]];
76 if (n1==n2) ans=mid,l=mid+1;
77 else r=mid-1;
78 }
79 return ans;
80 }
81 int main(){
82 #ifndef ONLINE_JUDGE
83 freopen("1014.in","r",stdin);
84 freopen("1014.out","w",stdout);
85 #endif
86 scanf("%s",s+1);
87 n=strlen(s+1);
88 mi[0]=1; F(i,1,n*3) mi[i]=mi[i-1]*131;
89 s[0]=s[n+1]=0;
90 Build(root,0,0,n+1);
91 m=getint();
92 char cmd[5],s1[5];
93 int x,y;
94 F(i,1,m){
95 scanf("%s",cmd);
96 if (cmd[0]==‘Q‘){
97 x=getint(); y=getint();
98 printf("%d\n",query(x,y));
99 }else if (cmd[0]==‘R‘){
100 x=getint(); scanf("%s",s1);
101 int pos=kth(root,x+1);
102 v[pos]=s1[0];
103 splay(pos);
104 }else{
105 x=getint()+1; scanf("%s",s1);
106 splay(kth(root,x)); splay(kth(root,x+1),root);
107 int &pos=c[c[root][1]][0];
108 New_node(pos,c[root][1],s1[0]);
109 Push_up(c[root][1]); Push_up(root);
110 }
111 }
112 return 0;
113 }