Solution in c++
#include <iostream> #include <algorithm> using namespace std; int sol=-1,sz; string n; int moves(string type){ string nn=n; int my=0,s=1,tax; if(n.find(type[0])==-1 || n.find(type[1])==-1 )return 0; int f1=nn.find(type[0]); while(nn[0]!=type[0]) { swap(nn[f1],nn[f1-1]); f1--; my++; } if(type=="00"){nn=nn.substr(1,nn.size()-1);s=0;} f1=nn.find(type[1]); while(nn[s]!=type[s] ) { swap(nn[f1],nn[f1-1]); f1--; my++; } if(nn[ nn.size()-1 ]=='0' && count(nn.begin(),nn.end(),'0')+2<nn.size()) { tax=nn.size()-1; while(nn[tax]=='0')tax--; my+=nn.size()-tax-1; } else if(nn[ nn.size()-1 ]=='0' && count(nn.begin(),nn.end(),'0')+2==nn.size())return -1; (sol==-1)? sol=my : sol=min(sol,my); } int main() { cin>>n; reverse(n.begin(),n.end()); sz=n.size(); if(count(n.begin(),n.end(),'0')>1)moves("00"); moves("52"); moves("05"); moves("57"); cout<<sol; return 0; }
0 التعليقات:
إرسال تعليق