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(组合数学)的更多相关文章

  1. poj1496 Word Index / poj1850 Code(组合数学)

    poj1850 Code 题意:输出若干个给定的字符串($length<=10$)在字典序中的位置,字符串中的字母必须严格递增. 读取到非法字符串时,输出“0”,终止程序.(poj1496:继续 ...

  2. poj1850 Code【组合数学】By cellur925

    题意: * 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序字符串)* a - 1* b - 2* ...* z - 26* ab - 27* ...* az - 51* bc - ...

  3. Code(组合数学)

    Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8766 Accepted: 4168 Description Tran ...

  4. poj1850 Code

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10059   Accepted: 4816 Description ...

  5. poj:1850 Code(组合数学?数位dp!)

    题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...

  6. POJ 1850:Code 组合数学

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8710   Accepted: 4141 Description ...

  7. POJ1850 Code(组合+康托展开)

    题目问一个合法字符串的字典序是第几个,合法的字符串是指里面的字符严格递增. 先判断合不合法,然后用类似康托展开的过程去求.大概过程就是用组合数算出某长度某前缀有几个,累加起来. 真难一遍写对.. #i ...

  8. poj 1850 code(组合数学)

    题目:http://poj.org/problem?id=1850 题意:按给定的规则给字母编号. 一个很简单的题目,但是却做了好久.................................. ...

  9. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

随机推荐

  1. jCallout 实现气泡提示

    在提交表单前.焦点转移后或者 keyup 时往往需要对输入的文本就行检验,如果输入内容不符合相关约定则要进行提示或警告,有一个叫 jCallout 的插件可以轻松实现该功能,该插件基于  jQuery ...

  2. 【转】TCP的SEQ和ACK的生成

    TCP序列号和确认号详解 完整的PDF下载: 在网络分析中,读懂TCP序列号和确认号在的变化趋势,可以帮助我们学习TCP协议以及排查通讯故障,如通过查看序列号和确认号可以确定数据传输是否乱序.但我在查 ...

  3. 线程模式HS/HA和L/F的区别, HS/HA的实现原理图

    线程池模式一般分为两种:L/F领导者与跟随者模式.HS/HA半同步/半异步模式. HS/HA 半同步/ 半异步模式 :分为三层,同步层.队列层.异步层,又称为生产者消费者模式,主线程处理I/O事件并解 ...

  4. Headfirst设计模式的C++实现——复合模式

    observer.h #ifndef _OBSERVER_H_ #define _OBSERVER_H_ #include <string> class Observer { public ...

  5. CentOs7&zookeeper

    下载安装包 wget http://www.apache.dataguru.cn/zookeeper/stable/zookeeper-3.4.6.tar.gz 解压 tar xzvf zookeep ...

  6. 用css3写出来的进度条

    夜深了,废话不多说,先上代码: <style> * { box-sizing: border-box } .wrapper { width: 350px; margin: 200px au ...

  7. JS禁用和启用鼠标滚轮滚动事件

    // left: 37, up: 38, right: 39, down: 40, // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: ...

  8. 如何设置eclipse开发的web工程自动发布到tomcat的webapps下?

    使用eclipse开发web工程,在配置好服务器(tomcat)之后运行该web工程,发现能正常运行.但是问题在于,当你打开tomcat路径\webapps时,会发现没有该web应用(你的web工程名 ...

  9. Oracle 动态视图1 V$LOCK

    v$lock显示数据库当前持有锁情况 Column Datatype Description SID NUMBER 会话ID TYPE VARCHAR2(2) 表示锁的类型.值包括TM,TX,等 ID ...

  10. 开博第一篇:DHT 爬虫的学习记录

    经过一段时间的研究和学习,大致了解了DHT网络的一些信息,大部分还是参会别人的相关代码,一方面主要对DHT爬虫原理感兴趣,最主要的是为了学习python,大部分是别人的东西原理还是引用别人的吧 DHT ...