1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 typedef double DB;
16 #define For(i, s, t) for(int i = (s); i <= (t); i++)
17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
18 #define MIT (2147483647)
19 #define INF (1000000001)
20 #define MLL (1000000000000000001LL)
21 #define sz(x) ((int) (x).size())
22 #define clr(x, y) memset(x, y, sizeof(x))
23 #define puf push_front
24 #define pub push_back
25 #define pof pop_front
26 #define pob pop_back
27 #define ft first
28 #define sd second
29 #define mk make_pair
30 inline void SetIO(string Name) {
31 string Input = Name+".in",
32 Output = Name+".out";
33 freopen(Input.c_str(), "r", stdin),
34 freopen(Output.c_str(), "w", stdout);
35 }
36
37 const int N = 100010, Mod = 9875321;
38 struct Node {
39 int Fa, Child[2];
40 int Dat, Sum, Hash;
41
42 Node() {
43 Fa = 0, clr(Child, 0), Dat = Sum = 0;
44 }
45
46 #define Fa(x) (Tr[x].Fa)
47 #define C(x, y) (Tr[x].Child[y])
48 #define Lch(x) (Tr[x].Child[0])
49 #define Rch(x) (Tr[x].Child[1])
50 #define Dat(x) (Tr[x].Dat)
51 #define Sum(x) (Tr[x].Sum)
52 #define Hash(x) (Tr[x].Hash)
53 } Tr[N];
54 int Tot, Root;
55 int m, Len;
56 int Fact[N];
57 string S;
58
59 inline void Input() {
60 cin>>S;
61 scanf("%d", &m);
62 }
63
64 inline void Updata(int x) {
65 Sum(x) = 1;
66 For(i, 0, 1) Sum(x) += Sum(C(x, i));
67 LL Cnt = Hash(Lch(x))*27+Dat(x);
68 Cnt = Cnt*Fact[Sum(Rch(x))]+Hash(Rch(x));
69 Hash(x) = Cnt%Mod;
70 }
71
72 inline void Rotate(int x, int T) {
73 int y, z;
74 y = Fa(x); z = Fa(y);
75 if(Lch(z) == y) Lch(z) = x;
76 else if(Rch(z) == y) Rch(z) = x;
77 Fa(x) = z;
78 C(y, !T) = C(x, T), Fa(C(x, T)) = y;
79 C(x, T) = y, Fa(y) = x;
80 Updata(y);
81 }
82
83 inline void Splay(int x, int Goal) {
84 int y, z, T1, T2;
85 while(Fa(x) != Goal) {
86 y = Fa(x); z = Fa(y);
87 T1 = (Lch(y) == x), T2 = (Lch(z) == y);
88 if(z == Goal) Rotate(x, T1);
89 else if(T1^T2) {
90 Rotate(x, T1);
91 Rotate(x, T2);
92 } else {
93 Rotate(y, T2);
94 Rotate(x, T1);
95 }
96 }
97 Updata(x);
98 if(!Goal) Root = x;
99 }
100
101 inline int Find(int w) {
102 int x = Root;
103 while(w) {
104 if(w <= Sum(Lch(x))) x = Lch(x);
105 else if(w == Sum(Lch(x))+1) break;
106 else {
107 w -= 1+Sum(Lch(x));
108 x = Rch(x);
109 }
110 }
111
112 Splay(x, 0);
113 return x;
114 }
115
116 inline void Insert(int w, int x) {
117 int L, R;
118 L = Find(w), R = Find(w+1);
119 Splay(L, 0); Splay(R, L);
120 Lch(R) = ++Tot;
121 Fa(Tot) = R, Dat(Tot) = x;
122 Splay(Tot, 0);
123 }
124
125 inline int Query(int L, int R) {
126 int x, y;
127 x = Find(L-1), y = Find(R+1);
128 Splay(x, 0); Splay(y, x);
129 return Hash(Lch(y));
130 }
131
132 inline int Work(int A, int B) {
133 if(A > B) swap(A, B);
134 int Left = 0, Right = Len-B+1, Mid, Ret, Hash1, Hash2;
135 A++, B++;
136 while(Left <= Right) {
137 Mid = (Left+Right)>>1;
138 Hash1 = Query(A, A+Mid-1);
139 Hash2 = Query(B, B+Mid-1);
140 if(Hash1 == Hash2) {
141 Ret = Mid;
142 Left = Mid+1;
143 } else Right = Mid-1;
144 }
145
146 return Ret;
147 }
148
149 inline void Change(int w, int x) {
150 int A;
151 A = Find(w);
152 Dat(A) = x;
153 Updata(A);
154 }
155
156 inline void Solve() {
157 Fact[0] = 1;
158 For(i, 1, N-1) Fact[i] = (Fact[i-1]*27)%Mod;
159
160 Tot = 2, Root = 1;
161 Rch(1) = 2, Sum(1) = 2;
162 Fa(2) = 1, Sum(2) = 1;
163
164 Len = S.length();
165 For(i, 0, Len-1) {
166 int x = S[i]-‘a‘+1;
167 Insert(i+1, x);
168 }
169
170 while(m--) {
171 char Ch = ‘ ‘, x = ‘ ‘;
172 while(!(Ch >= ‘A‘ && Ch <= ‘Z‘)) Ch = getchar();
173 int a, b, Ret;
174
175 if(Ch == ‘Q‘) {
176 scanf("%d%d", &a, &b);
177 Ret = Work(a, b);
178 printf("%d\n", Ret);
179 } else if(Ch == ‘I‘) {
180 scanf("%d", &a);
181 while(!(x >= ‘a‘ && x <= ‘z‘)) x = getchar();
182 b = x-‘a‘+1;
183 Insert(a+1, b);
184 Len++;
185 } else {
186 scanf("%d", &a);
187 while(!(x >= ‘a‘ && x <= ‘z‘)) x = getchar();
188 b = x-‘a‘+1;
189 Change(a+1, b);
190 }
191 }
192 }
193
194 int main() {
195 SetIO("1014");
196 Input();
197 Solve();
198 return 0;
199 }