Skip to content

Instantly share code, notes, and snippets.

@balamark
Created June 11, 2015 00:20
Show Gist options
  • Save balamark/858551ab5161910bda3f to your computer and use it in GitHub Desktop.
Save balamark/858551ab5161910bda3f to your computer and use it in GitHub Desktop.
correct
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
string s;
int n;
int dp[105][105][9];
int solve(int p, int take, int num){
if(p==n){
if(take>0 and num==0) return dp[p][take][num] = 1;
return dp[p][take][num] = 0;
}
if(dp[p][take][num]!=-1) return dp[p][take][num];
int t = (num*10)+(s[p]-'0');
//take digit at position p or not
int a = solve(p+1, take+1, t%8);
int b = solve(p+1, take, num);
return dp[p][take][num%8] = max(a,b);
}
// use recursion to print, neat!
void print(int pos,int take,int rem)
{
if(pos==n) return;
int t = rem*10+(s[pos]-'0');
if(solve(pos+1,take+1,t%8))
{
cout<<s[pos];
print(pos+1,take+1,t%8);
}
else print(pos+1,take,rem);
}
int main(){
cin>>s;
n = s.size();
memset(dp, -1, sizeof dp);
if(solve(0,0,0)==0){
cout<<"NO\n";
}
else{
cout<<"YES\n";
print(0,0,0) ;
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment