-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (81 loc) · 2.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! palindromy hashe
#include <bits/stdc++.h>
using namespace std;
template <int X, int MOD>
struct Hash{
vector<int> hash;
vector<int> pows;
Hash(){}
Hash(const string s){
//create hash
hash.resize(s.size());
hash[0] = s[0] - 'a' + 1;
for(int i = 1; i < s.size(); i++)
hash[i] = (1LL * hash[i-1] * X + s[i] - 'a' + 1) % MOD;
//precalculate pows
pows.resize(s.size() + 1);
pows[0] = 1;
for(int i = 1; i < s.size(); i++)
pows[i] = (1LL * pows[i-1] * X) % MOD;
}
int get(const int a, const int b){
if(a == 0)
return hash[b];
return ( 1LL * hash[b] - (1LL * hash[a-1] * pows[b - a + 1]) % MOD + MOD) % MOD;
}
};
template <int X, int MOD>
struct Query{
Hash<X, MOD> hash, hashr;
int n;
Query(string s){
n = s.size();
hash = Hash<X, MOD>(s);
reverse(s.begin(), s.end());
hashr = Hash<X, MOD>(s);
}
bool compare(int a1, int b1, int a2, int b2){
return hash.get(a1, b1) == hashr.get(n - b2 - 1, n - a2 - 1);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
const int X = 27, MOD = 1e9+7;
string s; cin >> s;
Query<X, MOD> obj(s);
int t; cin >> t;
while(t--){
int q, x;
cin >> q >> x;
x--;
if(q == 1){ //* nieparzysty [x]
int l = 0, r = x;
while(l < r){
int mid = (l+r)/2;
if(obj.compare(mid, x, x, x + x-mid))
r = mid;
else
l = mid + 1;
}
cout << (x - l) * 2 + 1 << '\n';
} else{ //* parzysty [x, x+1]
if(s[x] != s[x+1]){
cout << "-1\n";
}
else{
int l = 0, r = x;
while(l < r){
int mid = (l+r)/2;
if(obj.compare(mid, x, x + 1, x + 1 + x-mid))
r = mid;
else
l = mid + 1;
}
cout << (x - l) * 2 + 2 << '\n';
}
}
}
return 0;
}