Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<algorithm>
- #include<string>
- #include<string.h>
- #include<stdlib.h>
- #include<vector>
- #include<math.h>
- #include<map>
- #include<queue>
- using namespace std;
- bool check(string s){
- int n = (int)s.length();
- for(int i=0;i<n-3;i++){
- if(s.substr(i,4) == "2012") return true;
- }
- return false;
- }
- int main(){
- int n;
- string s;
- cin>>n>>s;
- map<string,int> mp;
- queue<string> q;
- q.push(s);
- int res = -1;
- mp[s] = 0;
- string ns;
- while(!q.empty()){
- string top = q.front();
- q.pop();
- if(check(top)){
- res = mp[top];
- break;
- }
- int n = (int)top.length();
- int step = mp[top];
- for(int i=0;i<n-1;i++){
- swap(top[i], top[i+1]);
- if(mp.find(top) == mp.end()){
- q.push(top);
- mp[top] = step+1;
- }
- swap(top[i], top[i+1]);
- }
- }
- printf("%d\n",res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement