UVA10912 Simple Minded Hashing【DP】

本文介绍了一个简单的哈希问题,涉及到将字母映射为数值并求和,导致可能出现碰撞。动态规划方法被用来计算长度为L且按升序排列的字母串中,映射值为S的字符串数量。例如,当L=3,S=10时,有4个符合条件的字符串。程序使用C++实现,通过初始化和动态规划数组计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 signed integers.
Sample Input
3 10
2 3
0 0
Sample Output
Case 1: 4
Case 2: 1

问题链接UVA10912 Simple Minded Hashing
问题简述:(略)
问题分析:动态规划问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10912 Simple Minded Hashing */

#include <bits/stdc++.h>

using namespace std;

const int M = 26;
const int N = 10000;
int dp[M + 1][M + 1][N + 1], sum[M + 1][N + 1];

void init()
{
    memset(dp, 0, sizeof dp);
    memset(sum, 0, sizeof sum);
    dp[0][0][0] = 1;
    for (int i = 1; i <= M; i++)
        for (int k = 1; k <= i * M; k ++)
            for (int j = 1; j <= min(k, M); j++) {
                for (int m = 0; m < j; m++)
                    dp[i][j][k] += dp[i - 1][m][k - j];
                sum[i][k] += dp[i][j][k];
            }
}

int main()
{
    init();

    int l, s, caseno = 0;
    while (scanf("%d%d", &l, &s) == 2 && (l || s))
        printf("Case %d: %d\n", ++caseno, sum[min(l, M)][s]);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值