UVa 10912
来源:互联网 发布:房地产 知乎 编辑:程序博客网 时间:2024/05/21 13:42
题目:定义一种小写字母组成的字符串的hash函数,函数值为字母的编号之和;
问对应长度为L且hash函数值为S的所有不同递增串的个数。
分析:动态规划(DP)。直接打表计算,查询输出即可。
首先:确定范围;
①递增串最长26,超过就非法,直接返回0;
②最大的和值为1+2+...+26 = 351,超过也是非法,直接返回0;
然后:进行DP;
①定义状态:f(l,e,s)为长度为l,函数值为s,以e结尾的串的个数;
②转移方程:f(l,e,s)= Σ f(l,t,s-e),其中t为小于e的字母;
说明:数据范围有点假O(∩_∩)O~。
#include <stdio.h>#include <stdlib.h>#include <string.h>int dp[30][30][400];int sum[30][400];int main(){memset(dp, 0, sizeof(dp));dp[0][0][0] = 1;for (int l = 1; l <= 26; ++ l) {for (int e = 1; e <= 26; ++ e) {for (int s = 0; s <= 351; ++ s) {for (int t = 0; t < e; ++ t) {dp[l][e][s+e] += dp[l-1][t][s];}}}}memset(sum, 0, sizeof(sum));for (int l = 1; l <= 26; ++ l) {for (int e = 1; e <= 26; ++ e) {for (int s = 0; s <= 351; ++ s) {sum[l][s] += dp[l][e][s];}}}int L, S, cases = 1;while (~scanf("%d%d",&L,&S) && L+S) {printf("Case %d: ",cases ++);if (L <= 26 && S <= 351) {printf("%d\n",sum[L][S]);}else {puts("0");}}return 0;}
阅读全文
0 0
- uva 10912
- UVa 10912
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- iOS之断言NSAssert
- json获取数据库的信息在前端页面显示
- ThinkPHP5 响应输出
- spring中applicationContext.xml在web.xml中的配置路径说明
- 逆序对
- UVa 10912
- 【C语言】数组
- 经济生活知识
- MySQL中的锁(表锁、行锁,共享锁,排它锁,间隙锁)
- 关于二维偏序的题
- 几种编码区别
- vs2012 error LNK2038 RuntimeLibrary 不匹配的解决
- HDU 1546 Idiomatic Phrases Game
- HttpClient学习整理