Processing math: 100%

[POJ1850] Code

Code
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10411   Accepted: 5005

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

Source

 

 
我们只要求出有多少个字符串小于输入的字符串就行了;
那么怎么求呢?
因为题目要求每一位都必须是递增的,而且每一个数字只能出现一次 ,那就意味着我们只要确定了有哪些字母,这个字符串也就唯一确定了;
所以我们可以用组合数来表示,C[26][i],表示从26个字母中取出i个字母的方案,这同时也是长度为i的字符串的方案;
设字符串长度为n, 我们就可以求出长度为1~n-1的方案总数;
接下来我们要考虑那长度为n的一部分;
对于位置i,我们可以填的字母范围是从上一个字母到这一位字母,开区间,即(str[i-1], str[i]);
我们设这一位选择的字母是ch, 那么我们的方案数就是C[26-ch][n-i-1],就是我们之后的所有字母都要大于ch, 所以可以选择的变成了26-ch个,然后我们已经填上了第i位,所以我们还需要选择出来的字母个数为n-i-1。
所以 ,我们得出 : 第i位的方案总数是 ∑(ch f str[i-1] to str[i]) ΣC[26-ch][n-i-1]。
最后,我们把小于n位的方案总数, 加上n位的方案总数, 再加上1就是答案;
 

 
Code:(很奇怪这道题就叫做Code...)
复制代码
#include <iostream>
#include <cstdio>
using namespace std;

int C[27][27];
int ans;

int main()
{
    string str;
    cin >> str;
    int n = str.length();
    for (register int i = 1 ; i < n ; i ++)
        if (str[i-1] >= str[i]) return puts("0"), 0;
    C[0][0] = 1;
    for (register int i = 1 ; i <= 26 ; i ++)
    {
        for (register int j = 0 ; j <= i ; j ++)
        {
            if (j == 0) C[i][j] = 1;
            else C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }
    for (register int i = 1 ; i < n ; i ++) ans += C[26][i];//n位之前 
    int last = 0;
    for (register int i = 0 ; i < n ; i ++)
    {
        int in = str[i] - 'a' + 1;
        for (register int j = last + 1 ; j < in ; j ++)
            ans += C[26-j][n-i-1];
        last = in;
    }
    cout<<ans + 1<<endl;
    return 0;
}
复制代码

 

 
 
posted @   zZhBr  阅读(158)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
阅读排行:
· 终于决定:把自己家的能源管理系统开源了!
· C#实现 Winform 程序在系统托盘显示图标 & 开机自启动
· 了解 ASP.NET Core 中的中间件
· 实现windows下简单的自动化窗口管理
· 【C语言学习】——命令行编译运行 C 语言程序的完整流程
点击右上角即可分享
微信分享提示