UVa - 10912 - Simple Minded Hashing-(dp)

2023-11-06 23:48
文章标签 dp simple uva hashing 10912 minded

本文主要是介绍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 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;
}




这篇关于UVa - 10912 - Simple Minded Hashing-(dp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359812

相关文章

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=