#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
struct minSegTree{
int tree[800002];
void init(int i, int l, int r){
tree[i] = INF;
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void update(int i, int l, int r, int x, int y){
if(l==r){
tree[i] = min(tree[i], y);
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, y);
else update(i*2+1, m+1, r, x, y);
tree[i] = min(tree[i*2], tree[i*2+1]);
}
int query(int i, int l, int r, int s, int e){
if(r<s || e<l) return INF;
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return min(query(i*2, l, m, s, e), query(i*2+1, m+1, r, s, e));
}
} minTree;
int n;
int arr[200002];
int k;
int b[4];
void input();
void solvek1();
void solvek2();
int main(){
input();
if(k==1) solvek1();
if(k==2) solvek2();
}
void input(){
string str;
cin >> str;
n = (int)str.size();
for(int i=1; i<=n; i++) arr[i] = (str[i-1] - '0');
cin >> str;
k = (int)str.size();
for(int i=1; i<=k; i++) b[i] = (str[i-1] - '0');
}
void solvek1(){
if(count(arr+1, arr+n+1, b[1])) printf("-1");
else printf("0");
exit(0);
}
void solvek2(){
int ans = 0;
if(b[1] != b[2]){
int l = b[2], r = b[1];
vector<int> v;
for(int i=1; i<=n; i++){
if(v.empty() || v.back() != arr[i]) v.push_back(arr[i]);
}
if(!v.empty() && v.back() == r) v.pop_back();
if(!v.empty() && v[0] == l) v.erase(v.begin());
ans = (int)v.size()/2;
}
else{
int x = b[1];
if(count(arr+1, arr+n+1, x) > (n+1)/2) ans = -1;
else{
for(int i=1; i<n; i++) if(arr[i] == x && arr[i+1] == x) ans++;
}
}
printf("%d", ans >= n*2 ? -1 : ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
5 ms |
1380 KB |
Output is correct |
4 |
Correct |
4 ms |
1380 KB |
Output is correct |
5 |
Correct |
3 ms |
1380 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
240 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
1380 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
8 |
Correct |
4 ms |
2332 KB |
Output is correct |
9 |
Correct |
4 ms |
1832 KB |
Output is correct |
10 |
Correct |
4 ms |
1832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
240 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
1380 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
8 |
Correct |
4 ms |
2332 KB |
Output is correct |
9 |
Correct |
4 ms |
1832 KB |
Output is correct |
10 |
Correct |
4 ms |
1832 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
4 ms |
1380 KB |
Output is correct |
21 |
Correct |
4 ms |
1380 KB |
Output is correct |
22 |
Correct |
4 ms |
2320 KB |
Output is correct |
23 |
Correct |
6 ms |
1836 KB |
Output is correct |
24 |
Correct |
5 ms |
1844 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
1380 KB |
Output is correct |
27 |
Correct |
4 ms |
1380 KB |
Output is correct |
28 |
Correct |
6 ms |
1384 KB |
Output is correct |
29 |
Correct |
4 ms |
1380 KB |
Output is correct |
30 |
Correct |
5 ms |
1380 KB |
Output is correct |
31 |
Correct |
4 ms |
1380 KB |
Output is correct |
32 |
Correct |
5 ms |
1380 KB |
Output is correct |
33 |
Correct |
0 ms |
276 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |