#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];
vector<int> loc; /// x�� �ִ� ��ġ��
loc.push_back(-1);
for(int i=1; i<=n; i++) if(arr[i] == x) loc.push_back(i);
loc.push_back(n+2);
vector<int> DP (loc.size(), INF);
DP[0] = 0;
vector<int> values (loc.size());
for(int i=0; i<(int)loc.size(); i++) values[i] = i+i-loc[i];
vector<int> valuesVec = values;
sort(valuesVec.begin(), valuesVec.end());
valuesVec.erase(unique(valuesVec.begin(), valuesVec.end()), valuesVec.end());
for(int i=0; i<(int)loc.size(); i++)
values[i] = lower_bound(valuesVec.begin(),valuesVec.end(),values[i]) - valuesVec.begin() + 1;
int L = (int)valuesVec.size();
minTree.init(1, 1, L);
for(int i=0; i<(int)DP.size(); i++){
if(i){
DP[i] = minTree.query(1, 1, L, values[i], L) + i - 1;
}
DP[i] = min(DP[i], INF);
if(DP[i] <= n*2) minTree.update(1, 1, L, values[i], DP[i] - i);
}
ans = DP.back();
}
printf("%d", ans >= n*2 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
1316 KB |
Output is correct |
4 |
Correct |
4 ms |
1380 KB |
Output is correct |
5 |
Correct |
5 ms |
1380 KB |
Output is correct |
6 |
Correct |
5 ms |
1380 KB |
Output is correct |
7 |
Correct |
4 ms |
1380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
4 ms |
2332 KB |
Output is correct |
9 |
Correct |
5 ms |
1832 KB |
Output is correct |
10 |
Correct |
5 ms |
1832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
1380 KB |
Output is correct |
7 |
Correct |
5 ms |
1400 KB |
Output is correct |
8 |
Correct |
4 ms |
2332 KB |
Output is correct |
9 |
Correct |
5 ms |
1832 KB |
Output is correct |
10 |
Correct |
5 ms |
1832 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1376 KB |
Output is correct |
21 |
Correct |
4 ms |
1380 KB |
Output is correct |
22 |
Correct |
5 ms |
2332 KB |
Output is correct |
23 |
Correct |
5 ms |
1832 KB |
Output is correct |
24 |
Correct |
6 ms |
1832 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
1380 KB |
Output is correct |
27 |
Incorrect |
20 ms |
2940 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |