当前位置: 代码迷 >> 综合 >> UVa - 10912 - Simple Minded Hashing-(dp)
  详细解决方案

UVa - 10912 - Simple Minded Hashing-(dp)

热度:21   发布时间:2024-01-12 15:42:26.0

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 10005int 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];elseans=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 10005int 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];elseans=0;printf("Case %d: %d\n",++cas,ans);}return 0;
}




  相关解决方案