首页 > 学院 > 开发设计 > 正文

组合数学 POJ 1850 Code

2019-11-14 12:36:39
字体:
来源:转载
供稿:网友

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

题目大意 符合题意的字符串要满足位于后面字母比前面大的条件,输入一个字符串,若不满足该条件,输出0,否则输出按照排序系统该字符串之前有多少个字符串。

解题思路 符合答案的字符串包括两种: 1、字符串长度小于输入字符串长度。标记输入字符串的长度为length,假设字符串为 i (i < length),则满足条件的值为c[26][i],可以理解为该情况为从26个字母中选取 i 个,从小到大依次排列。 2、字符串长度等于输入字符串长度。从左至右依次检索,对于每一位 i 而言,该位字符的取值范围应至少比前一位字符大一且小于当前字符,即 ch=[str[i-1]+1,str[i]);又因为该位字符之后剩余字符的取值范围为 ‘z’-ch,剩余字符长度为 length-i-1,即满足条件的值为 c[‘z’-ch][length-i-1]。

代码实现

#include <iostream>#include<cstdio>#include<cstring>using namespace std;int c[27][27];void init(){ for(int i=0; i<=26; i++) { for(int j=0; j<=i; j++) { if(!j||i==j) c[i][j]=1; else c[i][j]=c[i-1][j]+c[i-1][j-1]; } }}int main(){ char str[11]; int sum=0; scanf("%s%*c",str); init(); int length=strlen(str); for(int i=0; i<length-2; i++) { if(str[i]>str[i+1]) { PRintf("0/n"); return 0; } } for(int i=1; i<length; i++) sum+=c[26][i]; for(int i=0; i<length; i++) { char ch; if(i==0) ch='a'; else ch=str[i-1]+1; while(ch<str[i]) { sum+=c['z'-ch][length-i-1]; ch++; } } printf("%d/n",sum+1); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表