vjudge7701's blog

By vjudge7701, history, 3 years ago, In English

in this problem : https://mirror.codeforces.com/gym/104493/problem/A i know it is very slow to solve it using recursion but i want to know the solution using recursion to understand dp solution well here is my code it is WA2 :

int n;
int a[N] ;
ll sum=0;
ll mx=0;

int fr[11] ;
bool ok(int r){
    ll x = a[r] ;
    while(x > 0){
        fr[x%10]++;
        x/=10;
    }
    for(int i=0;i<10;i++){
        if(fr[i] > 2)return 0;
    }
    return 1;
}
void del(int r){
    ll x = a[r] ;
    while(x > 0){
        fr[x%10]--;
        x/=10;
    }
}
bool vis[N] ;
void rec(int r){
    if(r == n){
        mx  = max(mx , sum) ;
        return  ;
    }
    if(ok(r)){sum+=a[r];vis[r]=1;}
    else (del(r));
    rec(r + 1) ;
    if(vis[r]){sum-=a[r] ; vis[r]=0;del(r);}
    rec(r+1);

}
void solve(){
    cin >> n;
    for(int i=0;i<=n;i++)vis[i]=0;
    sum = mx = 0 ;
    for(int i=0;i<10;i++)fr[i]=0;
    for(int i=0;i<n;i++)cin>>a[i];
    rec(0);
    cout<<mx<<"\n";
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it