POJ1850——Code(组合数学)
Code
Description
Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that the words are made only of small characters of the English alphabet a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).
The coding system works like this:
• The words are arranged in the increasing order of their length.
• The words with the same length are arranged in lexicographical order (the order from the dictionary).
• We codify these words by their numbering, starting with a, as follows:
a - 1
b - 2
...
z - 26
ab - 27
...
az - 51
bc - 52
...
vwxyz - 83681
...
Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.
Input
The only line contains a word. There are some constraints:
• The word is maximum 10 letters length
• The English alphabet has 26 characters.
Output
The output will contain the code of the given word, or 0 if the word can not be codified.
Sample Input
bf
Sample Output
55
题目大意:
判断一个字符串在字典中的位置。
字符串必须保证为升序字符串才能合法。
解题思路:
组合数学问题。
首先通过dp打印出来com[][]杨辉三角形。
假设字符串的长度为len。
详情见备注。
Code:
/*************************************************************************
> File Name: poj1850.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月28日 星期二 01时32分11秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
using namespace std;
long long com[][];
void init() /*打印杨辉三角*/
{
for (int i=; i<=; i++)
for (int j=; j<=i; j++)
if (!j||i==j)
com[i][j]=;
else
com[i][j]=com[i-][j-]+com[i-][j];
com[][]=;
}
int main()
{
init();
char num[];
scanf("%s",num);
int len=strlen(num),k;
for (k=;k<=len-;k++) /*判断是否为递增字符串*/
if (num[k]<=num[k-]) break;
if (k!=len)
{
cout<<<<endl;
return ;
}
long long ans=;
/*任意长度小于等于len的数都在其前面*/
for (int i=; i<=len-; i++)
ans+=com[][i];
/*计算最高位为[a,num[0])的个数*/
for (char i='a'; i<num[]; i++)
ans+=com['z'-i][len-];
/*计算前j位相同的个数*/
for (int j=;j<=len-;j++)
/*第j位可能的数字为[num[j-1]+1,num[j]) */
for (char i=num[j-]+;i<num[j];i++)
ans+=com['z'-i][len--j];
cout<<ans+<<endl;
return ;
}
POJ1850——Code(组合数学)的更多相关文章
- 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 - ...
- Code(组合数学)
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8766 Accepted: 4168 Description Tran ...
- poj1850 Code
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10059 Accepted: 4816 Description ...
- poj:1850 Code(组合数学?数位dp!)
题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...
- POJ 1850:Code 组合数学
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8710 Accepted: 4141 Description ...
- POJ1850 Code(组合+康托展开)
题目问一个合法字符串的字典序是第几个,合法的字符串是指里面的字符严格递增. 先判断合不合法,然后用类似康托展开的过程去求.大概过程就是用组合数算出某长度某前缀有几个,累加起来. 真难一遍写对.. #i ...
- poj 1850 code(组合数学)
题目:http://poj.org/problem?id=1850 题意:按给定的规则给字母编号. 一个很简单的题目,但是却做了好久.................................. ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- day 0.
/* 嗯 就要结束了. OI生涯 2015.12-2016.11. 认识了很多人. 然后我这个学渣跟你们混在一起 感觉自卑至极啊. 好了 先不说这些伤心的话. Gryz小伙伴儿们NOIP RP++吧. ...
- Android 源码编译及常见错误及解决方法
最近要往arm开发板上移植android系统,大大小小的问题遇到了太多太多,都是泪啊.本人初接触嵌入式开发,对问题的根源不是太了解,不过好在每解决一个问题,便记录一下.话不多说,正式罗列问题: hos ...
- 将C# dataTable 做为参数传入到存储过程
1.list转换为DataTable(如果有需要) public static DataTable ListToDataTable<T>(List<T> entitys) { ...
- OWIN and Katana - 1
翻译自 http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana 十多年来,基于ASP.NET框 ...
- 医学影像工作站程序ProDicom的说明
转载 http://blog.csdn.net/prodicom/article/details/4015064 注意:以下内容为转载,但保留了第一人称,请注意,以免造成不必要的麻烦. 医网联影像工作 ...
- ASP.NET工具
每个开发人员现在应该下载的十种必备工具 发布日期: 7/20/2004 | 更新日期: 7/20/2004 本文自发布以来已经增加了新信息. 请参阅下面的编辑更新. 本文讨论: • 用于编写单元测试的 ...
- C# 返回json结果集,js日期格式化
asp.net mvc返回json结果集 return Json(new { total = totalCount, rows = result }, JsonRequestBehavior.Allo ...
- ofbiz进阶之实体引擎配置文件
The Open For Business Project: Entity Engine Configuration Guide 原文链接:http://ofbiz.apache.org/docs/e ...
- MySQL --slave-skip-errors
官方说明: --slave-skip-errors=[err_code1,err_code2,...|all] (MySQL Cluster NDB 7.0.33 and later; MySQL C ...
- go语言使用redis —— redigo
redis的client有好多好多,go语言的client在redis官方有两个推荐,radix和redigo.选择哪一个好呢?确实很纠结,后来掷硬币决定选择redigo了. redis.go.red ...