1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #define mod 9875321
8 using namespace std;
9 struct data {
10 int s[2];
11 int fa,size;
12 int v,val;
13 }t[150005];
14 int p[150005];
15 int cnt=0,rt;
16 char ch[150005];
17 int m;
18 int len;
19 inline void pushup(int x) {
20 int lc=t[x].s[0],rc=t[x].s[1];
21 t[x].size=t[lc].size+t[rc].size+1;
22 t[x].v=t[lc].v+(long long)t[x].val*p[t[lc].size]%mod+(long long)p[t[lc].size+1]*t[rc].v%mod;
23 t[x].v%=mod;
24 }
25 inline void rorate(int x,int &k) {
26 int y=t[x].fa,z=t[y].fa,l,r;
27 if(t[y].s[0]==x) l=0;else l=1;
28 r=l^1;
29 if(y==k) k=x;
30 else{if(t[z].s[0]==y) t[z].s[0]=x;else t[z].s[1]=x;}
31 t[x].fa=z;t[y].fa=x;t[y].s[l]=t[x].s[r];t[t[x].s[r]].fa=y;t[x].s[r]=y;
32 pushup(y);pushup(x);
33 }
34 inline void splay(int x,int &k) {
35 while(x!=k) {
36 int y=t[x].fa,z=t[y].fa;
37 if(y!=k) {
38 if(t[y].s[0]==x^t[z].s[0]==y) rorate(x,k);
39 else rorate(y,k);
40 }
41 rorate(x,k);
42 }
43 }
44 inline int find(int now,int x) {
45 if(t[t[now].s[0]].size+1<x) return find(t[now].s[1],x-t[t[now].s[0]].size-1);
46 else if(t[t[now].s[0]].size+1>x) return find(t[now].s[0],x);
47 else return now;
48 }
49 inline void insert(int x,int val) {
50 int t1=find(rt,x),t2=find(rt,x+1);
51 splay(t1,rt);splay(t2,t[rt].s[1]);
52 t[t2].s[0]=++cnt;t[cnt].val=val;t[cnt].fa=t2;
53 pushup(cnt);pushup(t2);pushup(t1);
54 }
55 inline void change(int now,int x,int val) {
56 if(t[t[now].s[0]].size+1<x) change(t[now].s[1],x-t[t[now].s[0]].size-1,val);
57 else if(t[t[now].s[0]].size+1>x) change(t[now].s[0],x,val);
58 else t[now].val=val;
59 pushup(now);
60 }
61 inline int query(int x,int y) {
62 int t1=find(rt,x-1);int t2=find(rt,y+1);
63 splay(t1,rt);splay(t2,t[rt].s[1]);
64 return t[t[t[rt].s[1]].s[0]].v;
65 }
66 inline bool check(int now,int x,int y) {
67 if(x+now-1>len) return 0;
68 if(y+now-1>len) return 0;
69 return query(x,x+now-1)==query(y,y+now-1);
70 }
71 int main() {
72 p[0]=1;for(int i=1;i<=150004;i++)p[i]=p[i-1]*27%mod;
73 scanf("%s",ch+1);
74 len=strlen(ch+1);
75 t[1].size=1;pushup(1);
76 for(int i=1;i<=len;i++) {
77 t[i].fa=i+1;t[i+1].s[0]=i;t[i+1].val=ch[i]-‘a‘+1;pushup(i+1);
78 }
79 t[len+1].fa=len+2;t[len+2].s[0]=len+1;t[len+2].val=0;rt=len+2;pushup(len+2);
80 cnt=len+2;
81 len++;
82 scanf("%d",&m);
83 for(int i=1;i<=m;i++) {
84 char h[10];
85 scanf("%s",h);
86 if(h[0]==‘Q‘) {
87 int x,y;
88 scanf("%d%d",&x,&y);
89 int l=1,r=len,ans=0;
90 while(l<=r) {
91 int mid=(l+r)>>1;
92 if(check(mid,x+1,y+1)) l=mid+1,ans=mid;
93 else r=mid-1;
94 }
95 printf("%d\n",ans);
96 }
97 else if(h[0]==‘I‘) {
98 int x;char y[10];
99 scanf("%d%s",&x,y);
100 insert(x+1,y[0]-‘a‘+1);
101 len++;
102 }
103 else if(h[0]==‘R‘) {
104 int x;char y[10];
105 scanf("%d%s",&x,y);
106 change(rt,x+1,y[0]-‘a‘+1);
107 }
108 }
109 }