poj 1850 Code

Code
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9366 Accepted: 4481

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


这题我真是很不容易的自己做了 出来,然后看了看题解,就觉得自己的思路是正确的但在求解上他们用的杨辉三角更方便,哎!还是技不如人啊!

/*

大致题意:

输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是 str前面所有字符串的个数 +1

规定输入的字符串必须是升序排列。不降序列是非法字符串

不要求用循环输入去输入若干组字符串,但若输入非法字符串则输出0,且结束程序

*/

我主要是用了一个二维数组a[i][j],i代表字符个数,j代表头字母的值,而具体的a[2][3](cz的值)就代表字符为两个字符开头为C的最后一个的数值,

然后我就由此发现,

a[1][1] = 1, a[1][2] = 2, a[1][3] = 3``````

a[2][1] = a[1][26]+ a[1][26]-a[1][1],,,,,,,,a[2][2] = a[2][1]+ a[1][26]-a[1][2],,,,,,

a[3][1] = a[2][25]+ a[2][25]-a[2][1],,,   这里为什么是25呢?因为yz就是字符为2的最后一个了,

a[3][2] = a[3][1]+ a[2][25]-a[2][2],

以上我们可以在简化一下

令a[2][0] = a[1][26],

这样就更方便了如a[2][1] = a[2][0] + a[1][26]-a[1][1],

规律你发现了吗?


然后我们怎样计算bf的值呢,我们就用a[2][2]-(a[1][26]-a[1][5])就可以了,明白吗?

以下是代码:

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

int a[100][100];

void init()
{
    int i, j;
    memset(a, 0, sizeof(a));
    for ( i = 1;i <= 26; i++ )
    {
        a[1][i] = i;
    }
    int h = 26;
    for ( i = 2;i <= 10; i++ )
    {
        a[i][0] = a[i-1][h];
        for ( j = 1; j <= h; j++ )
        {
            if ( j == 1 )
            {
                a[i][j] = a[i-1][h]+a[i-1][h]-a[i-1][j];
            }
            else
            {
                a[i][j] = a[i][j-1]+a[i-1][h]-a[i-1][j];
            }
        }
        h--;
    }
}

int Num(char ch[])
{
    int i;
    int len = strlen(ch);
    int sum = a[len][ch[0]-'a'+1];
    int sum2 = 0;
    for ( i = 1;i < len; i++ )
    {
        sum2 = sum2+a[len-i][26-len+i]-a[len-i][ch[i]-'a'+1]+1;
    }
    return sum-sum2;
}

int main()
{
    int i;
    char ch[15];
    init();
    while (~scanf ( "%s", ch ) )
    {
        int len=strlen(ch);
        for( i =1; i < len; i++ )
        {
            if( ch[i] <= ch[i-1] )
            {
                cout<<0<<endl;
                return 0;
            }
        }
        int sum = Num(ch);
        printf ( "%d\n", sum );
    }
}

代码菜鸟,如有错误,请多包涵!!!

如果有帮助记得支持我一下,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值