Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, June 29, 2014

LARSUBP-Large subsequence Problem

Large subsequence Problem

Given below code is for larsubp spoj or Large subsequence Problem spoj. 

Here the problem is simple we have to use DP .

Let given string be S then the number subsequence for any integer s[i] will be sum of all indices less then 'i' which are having integer less then s[i] + 1 (for s[i] )

let take an example s = 7598 

for 7 ans will be one because no character before it.

for 5 ans will be one because character before it is not less then 5;

for 9 ans is '3' because before it there are two character less then '9' so solution will be (ans for 7 + ans for 5 + 1)

similarly for 8 ans will be '3' two character before it are less then '8';

We can implement the above logic using hash table ;
See the python code for understanding .

If you found difficulty you can mail me @
Below given c++ code;

#include <bits/stdc++.h> 
using namespace std;
#define MOD 1000000007
int main() { 
    int t;
    scanf("%d ",&t); 
    for(int p=1;p<=t;p++) 
        char s[10009]; 
        int prev[11];
        for(int i=0;i<10;i++) 
            prev[i] =0; 
        int sum = 0; 
        int j=-1,k,temp;
            k = s[j] - 48; 
            temp = k; 
                prev[temp] = (prev[temp]+ prev[k])%MOD;
        for(int i=0;i<=9;i++)
            sum = (sum + prev[i])%MOD;
        printf("Case %d: %d\n",p,sum);
    return 0; 
Below given python code;

import sys
for p in xrange(1,t+1):
    for i in xrange(0,10):
    for i in xrange(0,len(s)):
        k = int(s[i])
        temp = k;
        while k>=0:
            pre[temp] = pre[temp] + pre[k]
    for i in xrange(0,10):
        su = su+pre[i]
    res =""
    su = su%1000000007
    res=res + "Case "+str(p)+": "+str(su)