人家都说是简单的组合数,但是我感觉没有说的那么简单啊、、、新手伤不起啊、、、
题意就是给你一串字符串,要求必须是递增的,如果不是的话就输出0;然后就是根据题目给出的样例求出组合数来。
就拿adf为例来解释一下:一开始有C(26,1)+C(26,2)就是说初始位置都是最小的时候的情况,在加上那些比原本初始位置最小值大的所有的情况,在特别处理一下s[0]就OK了啊、、、说的有点乱啊、、、建议自己多写几个数找找规律,就很好理解了啊。
PS:这是POJ第100个AC虽然数目有点少,但以后得好好刷题啊、、
Code
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7218 | Accepted: 3371 |
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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int c[30][30] = {0};
void _printf()
{
for(int i = 0; i <= 28; i++)
for(int j = 0; j <= i; j++)
if(!j || i == j)
c[i][j] = 1;
else
c[i][j] = c[i-1][j-1]+c[i-1][j];
}
int judge(string s)
{
int i;
int k = s.length();
for(i = 1; i < k; i++)
{
if(s[i] < s[i-1])
return 0;
}
return 1;
}
int main()
{
_printf();
int sum;
int i;
string s;
while(cin >>s)
{
sum = 0;
if(!judge(s))
{
cout <<'0'<<endl;
return 0;
}
int k = s.length();
for(i = 1; i < k; i++)
sum += c[26][i];
for(char str = 'a'; str != s[0]; str++)
sum += c['z'-str][k-1];//因为是递增的字符串,所以从剩下中的‘z’-str个中选出k-1个来填充剩下的位置。
for(i = 1; i < k; i++)
for(char str = s[i-1]+1; str != s[i]; str++)//因为要从当前位的开始第一位进行计算所以是s[i-1]+1;
sum += c['z'-str][k-1-i];
cout <<sum+1<<endl;
}
return 0;
}