-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcc_DIGJUMP.cpp
70 lines (61 loc) · 1.13 KB
/
cc_DIGJUMP.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
/*
Author : Abhinav
Modified : 27-08-2018 02:43:33 AM
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef map<ll,ll> mllmp;
typedef pair<int,int> PII;
#define faster ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define PB push_back
#define MP make_pair
# define INF 0x3f3f3f3f
const ll mod = 1e9+7;
#define rep(i,j,k) for(ll i=j; i<k; i++)
#define repv(i,j,k) for(ll i=j; i>k; i--)
int main(){
faster;
string s;
cin >> s;
ll sz = s.size();
vi v[10];
rep(i,0,sz)
v[s[i]-'0'].PB(i);
vi d(100005);
vector<bool> vis(100005);
d[0]=0;
vis[0]=true;
queue<ll> q;
q.push(0);
while(!q.empty()){
ll idx = q.front();
q.pop();
if(idx==sz-1)
break;
ll val = s[idx]-'0';
ll vsz = v[val].size();
rep(j,0,vsz){
ll tp=v[val][j];
if(!vis[tp]){
vis[tp]=true;
q.push(tp);
d[tp] = d[idx]+1;
}
}
v[val].clear();
if(idx-1>=0 && !vis[idx-1]){
vis[idx-1] = true;
q.push(idx-1);
d[idx-1] = d[idx]+1;
}
if(idx+1<sz && !vis[idx+1]){
vis[idx+1] = true;
q.push(idx+1);
d[idx+1] = d[idx]+1;
}
}
cout << d[sz-1];
return 0;
}