poj 1850 Code
Code
Time Limit: 1000MS | Memory Limit: 30000KB | 64bit IO Format: %I64d & %I64u |
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.
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.
• 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
/*
//poj1850
题意:
输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是在str前面所有字符串的个数+1
规定输入的字符串必须是升序排列。不降序列是非法字符串
解答太详细了
http://hi.baidu.com/you289065406/blog/item/d0900ff85e4b0b71024f563a.html#0
折服了
膜拜
竟然数学使此题变得如此简答
组合数学题,不知道为什么会被归类到递推数学,可能是因为杨辉三角和组合数之间的关系。。
*/
#include <iostream>
#include <string>
using namespace std;
char str[11];
int ta[27][27]={0};
void get_table()//利用了杨慧三角形
{
int i,j;
for(i=0;i<=26;i++)
{
for(j=0;j<=i;j++)
{
if(!j||i==j)ta[i][j]=1;
else ta[i][j]=ta[i-1][j-1]+ta[i-1][j];
}
}
}
int main()
{
int len,i,j,res;
get_table();
char ch;
while(cin>>str)
{
len=strlen(str);
for(i=1;i<len;i++)
{
if(str[i]<=str[i-1])
{
cout<<0<<endl;return 0;
}
}
res=0;
for(i=1;i<len;i++)//求nC1到nClen-1的和
{
res+=ta[26][i];
}
for(i=0;i<len;i++)
{
ch=(!i)?'a':str[i-1]+1;//要比前面的字符大1
while(ch<=str[i]-1) //ch<=str[i]-1根据升序规则,当前位置的ch最多只能比str这个位置实际上的字符小1
{
res+=ta['z'-ch][len-1-i]; //'z'-ch:小于等于ch的字符不允许再被选择,所以当前能够选择的字符总数为'z'-ch
ch++; //len-1-i :ch位置后面(不包括ch)剩下的位数,就是从'z'-ch选择len-1-i个字符
}
}
cout<<res+1<<endl;
}
return 0;
}