POJ 1850 Code(找规律)

                                    Code

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7913   Accepted: 3709

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//首先观察规律
1+1.....1 +
(25+...+1)+
((24+..+1) + (23+..+1)+..+1) 
+
[(24+..+1) + (23+..+1)+..+1)]+[(23+..+1)+..+1)]+...[1]+....//所以我维护了一个num[10][27]的数组
for(i=26;i>n;i--)
    {
        sum+=num[n-1][i];
        num[n][i]=sum;
    }num[i]中的所有数字相加  就是 代表 第i层完成之后的编号:举例:bcd这说明 前面的a开头的肯定完整了 所以这时候就是需要num[0]+num[1]所有元素的和 +num[2][1](代表三个字符的以a开头的所有数量) =bcd=26+325+300=651  然后加上bcd自己 就是652

这只是第一层上面的字母对于a偏移了  ,如果  第n层对第n-1层偏移的话  如aef  e对b偏移了 那么这个怎么算了我们可以忽略a 因为这里a对其不产生影响,唯一的影响是对e的起始的偏移位置的影响 所以我们可以用num[2][b-‘a‘+数组中起始有值的位置]+num[2][c-‘a‘+数组中起始有值的为位置]+....
/*
26 +
(25+...+1)
(25*24/2 +24*23/2 +...) +
( (24*23/2 +...) +(23*22/2+.....) )
*/
#include<stdio.h>
#include<string.h>
__int64 num[10][27];
void dfs(__int64 n)
{
    __int64 i;
    if(n>10) return ;

    __int64 sum=0;
    for(i=26;i>n;i--)
    {
        sum+=num[n-1][i];
        num[n][i]=sum;
    }
    dfs(n+1);
}
int main(void)
{
    __int64 i,j;
    char str[15];
    for(i=0;i<27;i++) num[0][i]=1;
    dfs(1);
    while(scanf("%s",&str[1])!=EOF)
    {
        __int64 len=strlen(str)-1;
        __int64 tol=0;
        //除掉不满足情况的
        for(i=2;i<=len;i++)
        {
            if(str[i]<=str[i-1]){ printf("0\n");
            return 0;}
        }

        for(i=0;i<len-1;i++)//先算总层
        {
            for(j=26;j>=i;j--)
            {    tol+=num[i][j];
            }
        }
        str[0]=‘a‘-2;//起始的时候处理一下
        for(j=len;j>0;j--){
            for(i=0;i<str[len-j+1]-(str[len-j]+1);i++)
            {
                tol+=num[j-1][j+i+(str[len-j]+1-‘a‘)];
            }
        }

        printf("%I64d\n",tol);
    }
    return 0;
}

ps:woshi1993

时间: 2024-12-11 23:08:12

POJ 1850 Code(找规律)的相关文章

POJ 1850 Code 数位DP

据说又是一道组合数学题,数学不好的我只想出的DP写法 注意如果输入不合法要输出0 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdli

POJ 1850 Code 组合数学

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

[POJ 1850] Code

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8539   Accepted: 4048 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

POJ 1850 Code(组合数学)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8662   Accepted: 4113 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

poj:1850 Code(组合数学?数位dp!)

题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][pre]前pos位,前一位是pre有几个满足条件的字符串,其实等同于这个字符串的序号是多少 好像数位dp的博客真没什么东西好写的... #include<iostream> #include<cstring> #include<cstdlib> #include<cs

POJ 1850/ 1496 组合数学

POJ 1850/ 1496 组合数学 题目地址: POJ 1496 Word Index POJ 1850 Code 题意: 1. 每个词是自增的 2. 同样长度的词是按字典序排练的 3. 我们把这些词标序 求某个词的序号 分析: 组合数学... 推出公式然后用杨辉三角打表出组合数,具体看http://blog.csdn.net/lyy289065406/article/details/6648492. 我还是太弱Orz... 代码: 1496: /* * Author: illuz <iil

poj 1781 In Danger(约瑟夫环,找规律)

http://poj.org/problem?id=1781 约瑟夫环的模板,每次数到2的人出圈. 但直接求会TLE,n太大. 打表发现答案和n有关系.当n是2的幂的时候,答案都是1,不是2的幂的时候都与小于2的幂那个数相差差值的2的倍数. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack&

POJ 1870 Bee Breeding(找规律)

题目链接 题意 : 给你一个蜂巢状图形,让你找出两个点之间的距离. 思路 : 在做这个题之前可以看一下2265,因为是一种题来着,规律就是我在2265里写的那样,然后就是求距离了,求距离的时候只需考虑两个点的坐标差值(x,y),把坐标差值分成四个项限,x>0且y>0,或x<0且y<0为abs(x+y),其他情况则是max(abs(x),abs(y)).. 1 #include <cstdio> 2 #include <cstring> 3 #include

【POJ 1850】 Code

[POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az下位为bc 上位必须比下位小 依据这个规则搜出全部情况就可以 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int dp[11][27]; int digit[11