UVa - 10912 - Simple Minded Hashing-(dp)

All of you know a bit or two about hashing. It involves mapping an element into a numerical value using some mathematical function. In this problem we will consider a very ‘simple minded hashing’. It involves assigning numerical value to the alphabets and summing these values of the characters.

For example, the string “acm” is mapped to 1 + 3 + 13 = 17. Unfortunately, this method does not give one-to-one mapping. The string “adl” also maps to 17 (1 + 4 + 12). This is called collision.

In this problem you will have to find the number of strings of length L, which maps to an integer S, using the above hash function. You have to consider strings that have only lowercase letters in strictly ascending order.

Suppose L = 3 and S = 10, there are 4 such strings.

  1. abg
  2. acf
  3. ade
  4. bce

agb also produces 10 but the letters are not strictly in ascending order.

bh also produces 10 but it has 2 letters.

Input

There will be several cases. Each case consists of 2 integers L and S(0 < L, S < 10000). Input is terminated with 2 zeros.

Output

For each case, output Case #: where # is replaced by case number. Then output the result. Follow the sample for exact format. The result will fit in 32 bit signed integers.

Sample Input

Output for Sample Input

3 10
2 3

0 0

Case 1: 4
Case 2: 1



题意:字母a~z对应数字1~26,给出l,s,让求长度为l的严格上升字符串序列并且其和为s的方案数

那么由题中严格上升序列这一条件知当l>26或s>351时,结果为0;

用dp来做,方法一:
dp[i][j][k]代表用前i个字符串组成长度为j的串其和为s的方案数,那么dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-i];

dp[i-1][j][k]即为用前i-1个字符串组成长度为j的串其和为s的方案数,说明字符i使用了

dp[i-1][j-1][k-i]即为使用了i字符时产生的方案数

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 10005

int l,s;
int ans;
int dp[30][30][355];

void init()
{
    int i,j,k;
    dp[0][0][0]=1;
    for(i=1;i<=26;i++)
    {
        for(j=0;j<=i;j++)
        {
            for(k=0;k<=351;k++)
            {
                dp[i][j][k]=dp[i-1][j][k];
                if(j&&k>=i)
                    dp[i][j][k]+=dp[i-1][j-1][k-i];
            }
        }
    }
}

int main()
{
    init();

    int cas=0;
    while(scanf("%d%d",&l,&s)!=EOF)
    {
        if(l==0&&s==0) break;
            
        if(l<=26&&s<=351)
            ans=dp[26][l][s];
        else
            ans=0;
            
        printf("Case %d: %d\n",++cas,ans);

    }

    return 0;
}


方法二:

dp[l][[e][s]代表l个字符,和为s,以e结尾的字符串的个数,那么dp[l][e][s]=∑(0≤t<e) dp[l - 1][t][s-e]; 表示在l-1个字符,和为s-e,以t结尾的字符串的个数末尾加上一个权值为e的字符。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 10005

int l,s;
int ans;
int dp[30][30][355];
int num[30][355];

void init()
{
    int l,s,e,t;
    dp[0][0][0]=1;
    for(l=1;l<=26;l++)
    {
        for(e=1;e<=26;e++)
        {
            for(s=0;s<=351;s++)
            {
                for(t=0;t<e;t++)
                    dp[l][e][s+e]+=dp[l-1][t][s];
            }
        }
    }

    for(l=1;l<=26;l++)
    {
        for(e=1;e<=26;e++)
        {
            for(s=0;s<=351;s++)
            {
                num[l][s]+=dp[l][e][s];
            }
        }
    }

}

int main()
{
    init();

    int cas=0;
    while(scanf("%d%d",&l,&s)!=EOF)
    {
        if(l==0&&s==0) break;

        if(l<=26&&s<=351)
            ans=num[l][s];
        else
            ans=0;

        printf("Case %d: %d\n",++cas,ans);

    }

    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值