POJ1850 组合数学
问题重述:
用26个小写字母进行编码,编码规则如下:
1)每个编码中前一个字母必须小于后一个字母
2)编码按照长度从小到大排列,相同长度按字典序进行排列
输入一个字母串,求解该编码对应的数值。
问题分析:
该问题等价于求解小于输入编码的编码的数目。
对于编码X = x1,x2,x3,...xk, 小于X的编码可以分为两个部分
1)位数小于k的编码。
这部分编码的数目 = C[26][1] + C[26][1] + ... + C[26][k - 1]
2)长度为k,且小于X的编码。
假设Y为满足该条件的编码,现只需确定Y的数目。从左到右遍历编码X: i = 1 to k,假设X和Y的前i - 1位均相等且 yi != xi,那么 yi 必须满足 xi-1 = yi - 1 < yi < xi。
对于yi的每一种取值, yi, yi + 1, ... yk只需满足递增关系即可, 共有C[26][25 - (yi - 'a')]种编码。
根据以上分析,即可求出结果。
AC代码:
//Memory: 204K Time: 0MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
string s, ss;
][];
void init()
{
; i <= ; i++)
c[i][] = c[i][i] = ;
; i <= ; i++)
; j < i; j++)
c[i][j] = c[i - ][j] + c[i - ][j - ];
}
int main()
{
cin >> s;
int len = s.size();
; i < len - ; i++) {
] || s[i] == s[i + ]){
cout << << endl;
;
}
}
init();
;
; i <= len - ; i++) {
ans += c[][i];
}
; i < s[] - 'a'; i++) {
ans += c[ - i][len - ];
}
; i < len; i++) {
] - ; j < s[i] - 'a'; j++)
ans += c[ - j][len - - i];
}
cout << ans + << endl;
;
}
POJ1850 组合数学的更多相关文章
- POJ1850——Code(组合数学)
Code DescriptionTransmitting and memorizing information is a task that requires different coding sys ...
- poj1496 Word Index / poj1850 Code(组合数学)
poj1850 Code 题意:输出若干个给定的字符串($length<=10$)在字典序中的位置,字符串中的字母必须严格递增. 读取到非法字符串时,输出“0”,终止程序.(poj1496:继续 ...
- poj1850 Code【组合数学】By cellur925
题意: * 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序字符串)* a - 1* b - 2* ...* z - 26* ab - 27* ...* az - 51* bc - ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- 组合数学or not ---- n选k有重
模板问题: 1. 取物品 (comb.pas/c/cpp) [问题描述] 现在有n个物品(有可能相同),请您编程计算从中取k个有多少种不同的取法.[输入] 输入文件有两行,第一行包含两个整数n,k(2 ...
- 组合数学(全排列)+DFS CSU 1563 Lexicography
题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...
- uestc1888 Birthday Party 组合数学,乘法原理
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25539#problem/G 题目意思: 有n个人,每个人有一个礼物,每个人能拿 ...
- UVA 11076 Add Again 计算对答案的贡献+组合数学
A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...
- POJ3252——Round Number(组合数学)
Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...
随机推荐
- MySql 到 SQL Server(MSSQL)
我用 MySql 2年了,一直都是 asp.net + MySql 开发 为什么不用 MSSQL呢? 原因 : 在我的国家,几乎找不到任何一间托管公司有提供 SQL SERVER 的,有的话也是 20 ...
- 五个新知识:微软SHA2补丁,亚信专业工具,微软官方文档,使用过期签名(附官方推荐链接),注意使用具有UAC的CMD
五个新知识:微软SHA2补丁,亚信专业工具,微软官方文档,使用过期签名 不支持SHA2算法的计算机更新补丁:https://technet.microsoft.com/zh-CN/library/se ...
- BZOJ3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛
3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 30 Solved: 17[Sub ...
- BZOJ1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富
1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 459 Sol ...
- HDOJ 1323 Perfection(简单题)
Problem Description From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c a ...
- NuGet Package 管理工具
http://npe.codeplex.com/downloads/get/clickOnce/NuGetPackageExplorer.application
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- sicily 4378 connected components in undirected graph
题意:求图中的连通块数,注意孤立的算自连通! 例如:6个顶点3条路径,其中路径为:1->2 4->5 1->3 那么有(1-2&&1->3) + (4- ...
- vmware vms migration to openstack
Converting a VMware Workstation virtual machine to KVM Leave a commentPosted by rbgeek on August 13, ...
- 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
此文章转载自:http://www.myexception.cn/mobile/1259076.html 关于javax.crypto.BadPaddingException: Blocktype异常 ...