Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n;
- void n2(int& ans,string& s)
- {
- if(s[0]=='1' && s[1]=='1')
- {
- ans+=2;
- s[0]='0',s[1]='0';
- }
- else if(s[0]=='1' && s[1]=='0')
- {
- ans+=3;
- s[0]='0';
- }
- else if(s[0]=='0' && s[1]=='1')
- {
- ans+=1;
- s[1]='0';
- }
- }
- void func(int ind, string& s, bool flip, int& ans,int pos)
- {
- if(ind==(n-3))
- {
- if(s[ind]=='1' && s[ind+1]=='1' && s[ind+2]=='1')
- {
- if(!flip)
- {
- ans+=5;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=2;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='1' && s[ind+1]=='1' && s[ind+2]=='0')
- {
- if(!flip)
- {
- ans+=4;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=3;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='1' && s[ind+1]=='0' && s[ind+2]=='1')
- {
- if(!flip)
- {
- ans+=4;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=3;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='1' && s[ind+1]=='0' && s[ind+2]=='0')
- {
- if(!flip)
- {
- ans+=7;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=0;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='0' && s[ind+1]=='1' && s[ind+2]=='1')
- {
- if(!flip)
- {
- ans+=2;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=5;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='0' && s[ind+1]=='1' && s[ind+2]=='0')
- {
- if(!flip)
- {
- ans+=3;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=4;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else if(s[ind]=='0' && s[ind+1]=='0' && s[ind+2]=='1')
- {
- if(!flip)
- {
- ans+=1;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=6;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- else
- {
- if(!flip)
- {
- ans+=0;
- s[ind]='0',s[ind+1]='0',s[ind+2]='0';
- }
- else
- {
- ans+=7;
- s[ind]='1',s[ind+1]='0',s[ind+2]='0';
- }
- }
- return;
- }
- if(flip)
- {
- ans+=1;
- if((pos+1 == ind+1) && (ind+1 != n-3))
- {
- if(s[ind+1]=='1')
- func(ind+1,s,false,ans,pos);
- else
- {
- func(ind+1,s,true,ans,pos);
- s[ind+1]='1';
- }
- }
- else if((pos+1 == ind+1) && (ind+1 == n-3))
- func(ind+1,s,true,ans,pos);
- else
- {
- func(ind+1,s,false,ans,pos);
- s[ind+1]='0';
- }
- }
- else
- {
- if(ind+1 == n-3)
- func(ind+1,s,false,ans,pos);
- else
- {
- if(s[ind+1]=='1')
- {
- func(ind+1,s,true,ans,pos);
- s[ind+1]='0';
- }
- else
- func(ind+1,s,false,ans,pos);
- }
- }
- return;
- }
- main()
- {
- string s;cin>>s;
- n=s.length();int ans=0;
- if(n==1)
- {
- if(s[0]=='1')
- ans++;
- cout<<ans<<endl;
- }
- else if(n==2)
- {
- n2(ans,s);
- cout<<ans<<endl;
- }
- else
- {
- for(int i=0;i<n-2;i++)
- {
- if(i==(n-3) && s[i]=='1')
- {
- func(i,s,false,ans,i);
- s[i]='0';
- }
- if(s[i]=='1')
- {
- func(i,s,true,ans,i);
- s[i]='0';
- }
- cerr<<s<<endl;
- cerr<<"ans: "<<ans<<endl;
- }
- cout<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement