1 const maxn=100000+10;
2 p=9875321;
3 var i,n,m,x,t,y,z,l,r,rt,mid,tot:longint;
4 ch:char;
5 st:ansistring;
6 s,v,w,fa,a:array[0..maxn] of int64;
7 c:array[0..maxn,0..1] of longint;
8 procedure pushup(x:longint);
9 begin
10 s[x]:=s[c[x,0]]+s[c[x,1]]+1;
11 w[x]:=(w[c[x,0]]*a[s[c[x,1]]+1] mod p+
12 v[x]*a[s[c[x,1]]] mod p
13 +w[c[x,1]] mod p) mod p;
14 end;
15 procedure rotate(x:longint;var k:longint);
16 var l,r,y,z:longint;
17 begin
18 y:=fa[x];z:=fa[y];
19 l:=ord(c[y,1]=x);r:=l xor 1;
20 if y=k then k:=x else c[z,ord(c[z,1]=y)]:=x;
21 fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y;
22 c[y,l]:=c[x,r];c[x,r]:=y;
23 pushup(y);pushup(x);
24 end;
25 procedure splay(x:longint;var k:longint);
26 var y,z:longint;
27 begin
28 while x<>k do
29 begin
30 y:=fa[x];z:=fa[y];
31 if y<>k then
32 if (c[z,0]=y) xor (c[y,0]=x) then rotate(x,k)
33 else rotate(y,k);
34 rotate(x,k);
35 end;
36 end;
37 function find(x,rank:longint):longint;
38 var l,r:longint;
39 begin
40 l:=c[x,0];r:=c[x,1];
41 if s[l]+1=rank then exit(x)
42 else if s[l]>=rank then exit(find(l,rank))
43 else exit(find(r,rank-s[l]-1));
44 end;
45 procedure del(k:longint);
46 var x,y,z:longint;
47 begin
48 x:=find(rt,k-1);y:=find(rt,k+1);
49 splay(x,rt);splay(y,c[x,1]);
50 z:=c[y,0];fa[z]:=0;s[z]:=0;c[y,0]:=0;
51 pushup(y);pushup(x);
52 end;
53
54 procedure build(l,r,f:longint);
55 var mid,now,last:longint;
56 begin
57 if l>r then exit;
58 mid:=(l+r)>>1;
59 now:=mid;last:=f;
60 fa[now]:=last;
61 c[last,ord(mid>f)]:=now;
62 if l=r then
63 begin
64 s[now]:=1;w[now]:=v[now];
65 exit;
66 end;
67 build(l,mid-1,mid);build(mid+1,r,mid);
68 pushup(now);
69 end;
70
71 procedure init;
72 begin
73 a[0]:=1;
74 for i:=1 to maxn do a[i]:=(a[i-1]*31) mod p;
75 readln(st);n:=length(st);
76 for i:=2 to n+1 do v[i]:=ord(st[i-1])-ord(‘a‘)+1;
77 build(1,n+2,0);rt:=(n+3)>>1;
78 end;
79 function check(l,r,len:longint):boolean;
80 var x,y,xx,yy:longint;
81 begin
82 if len=0 then exit(true);
83 x:=find(rt,l);y:=find(rt,l+len+1);
84 splay(x,rt);splay(y,c[x,1]);
85 xx:=w[c[y,0]];
86 x:=find(rt,r);y:=find(rt,r+len+1);
87 splay(x,rt);splay(y,c[x,1]);
88 yy:=w[c[y,0]];
89 if xx=yy then exit(true) else exit(false);
90 end;
91 function query(x,y:longint):longint;
92 var l,r,mid:longint;
93 begin
94 if x>y then begin t:=x;x:=y;y:=t;end;
95 if x=y then exit(tot-y-1);
96 if not(check(x,y,1)) then exit(0);
97 if check(x,y,tot-y-1) then exit(tot-y-1);
98 l:=2;r:=tot-y-1;
99 while l<r do
100 begin
101 mid:=(l+r)>>1;
102 if check(x,y,mid) then l:=mid+1 else r:=mid;
103 end;
104 exit(l-1);
105 end;
106 procedure main;
107 begin
108 readln(m);tot:=n+2;
109 for i:=1 to m do
110 begin
111 read(ch);
112 case ch of
113 ‘R‘:begin
114 read(x);read(ch);readln(ch);
115 x:=find(rt,x+1);
116 splay(x,rt);
117 v[x]:=ord(ch)-ord(‘a‘)+1;
118 pushup(x);
119 end;
120 ‘I‘:begin
121 read(z);read(ch);readln(ch);
122 x:=find(rt,z+1);y:=find(rt,z+2);
123 splay(x,rt);splay(y,c[x,1]);
124 inc(tot);c[y,0]:=tot;fa[tot]:=y;s[tot]:=1;
125 v[tot]:=ord(ch)-ord(‘a‘)+1;w[tot]:=v[tot];
126 pushup(y);pushup(x);
127 end;
128 ‘Q‘:begin
129 readln(x,y);
130 writeln(query(x,y));
131 end;
132 end;
133 end;
134 end;
135 begin
136 assign(input,‘input.txt‘);assign(output,‘output.txt‘);
137 reset(input);rewrite(output);
138 init;
139 main;
140 close(input);close(output);
141 end.
142