POJ 1496 POJ 1850 组合计数

Code

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 8256 Accepted: 3906

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

Romania OI 2002

/**************************************
     author     :  Grant Yuan
     time        :  2014/10/12 17:06
     algortihm: 组合计数
     source     : POJ 1496 POJ 1850
***************************************/

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char a[27];
int num[27][27];
long long ans;
bool check()
{
    int l=strlen(a);
    if(l==1) return 1;
    for(int i=0;i<l-1;i++)
        if(a[i]>=a[i+1]) return 1;
    return 0;
}
void Get_num()
{
    int l=strlen(a);
    for(int i=0;i<=26;i++)
        for(int j=0;j<=i;j++)
    {
        num[i][j]=0;
        if(i==0||j==0) num[i][j]=1;
        else num[i][j]=num[i-1][j]+num[i-1][j-1];
    }
}
void  Get_sum1()
{
    int l=strlen(a);
    ans=0;
    for(int i=0;i<l;i++)
        ans+=num[26][i];
}
void Get_sum2()
{
    int l=strlen(a);
   for(int i=0;i<l;i++)
   {
       char j;
       if(i==0) j='a';
       else j=a[i-1]+1;
       for(;j<a[i];j++)
       {
           ans+=num['z'-j][l-i-1];
       }
   }
}
int main()
{
    Get_num();
    while(~scanf("%s",a)){
        int l;
        l=strlen(a);
        if(l==1) {printf("%d\n",a[0]-'a'+1);continue;}
        if(check()) {printf("0\n");continue;}
        Get_sum1();
        Get_sum2();
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-17 07:03:59

POJ 1496 POJ 1850 组合计数的相关文章

POJ 2249-Binomial Showdown(排列组合计数)

Binomial Showdown Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18457   Accepted: 5633 Description In how many ways can you choose k elements out of n elements, not taking order into account? Write a program to compute this number. Inp

POJ 3252 组合计数

Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9149 Accepted: 3248 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro,

POJ 1019 组合计数

链接:POJ 1019 /***************************************** author : Grant Yuan time : 2014/10/19 14:38 source : POJ 1019 algorithm: 组合计数 ******************************************/ #include <iostream> #include <cstdio> #include <algorithm> #

poj 2154 Color(polya计数 + 欧拉函数优化)

http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目,旋转后一样的属于同一种,结果模p. n个珠子应该有n种旋转置换,每种置换的循环个数为gcd(i,n).如果直接枚举i,显然不行.但是我们可以缩小枚举的数目.改为枚举每个循环节的长度L,那么相应的循环节数是n/L.所以我们只需求出每个L有多少个i满足gcd(i,n)= n/L,就得到了循环节数为n/L的个数.重点就是求出这样的i的个数. 令cnt = gcd(i,n) =

poj 3903 &amp; poj 2533 最长上升子序列(LIS)

最长上升子序列. 做这道题之前先做了2533,再看这道题,感觉两道题就一模一样,于是用2533的代码直接交, TLE了: 回头一看,数据范围.2533 N:0~1000:3903 N :1~100000. 原因终归于算法时间复杂度. 也借这道题学习了nlgn的最长上升子序列.(学习链接:http://blog.csdn.net/dangwenliang/article/details/5728363) 下面简单介绍n^2 和 nlgn 的两种算法. n^2: 主要思想:DP: 假设A1,A2..

半平面交 模板 poj 3335 poj 3130 poj 1474 判断半平面交是否为空集

半平面交模板 const double pi= acos(-1.0); #define arc(x) (x / 180 * pi) const double EPS = 1e-8; const int Max_N = 105; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point p){ return Point(x- p.x , y - p.y ) ;

Yue Fei&#39;s Battle(组合计数递推)

//求一个直径为 k 的树有多少种形态,每个点的度不超过 3 // 非常完美的分析,学到了,就是要细细推,并且写的时候要细心 还有除法取模需要用逆元 #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> using namespace std; #define MOD 1000000007 #define L

POJ 3670 &amp;&amp; POJ 3671 (dp)

最长不下降子序列的应用嘛.两题都是一样的. POJ 3670:求给定序列按递增或递减排列时,所需改变的最小的数字的数目. POJ 3671:求给定序列按递增排列时,所需改变的最小的数字的数目. 思路就是求最长不下降子序列,然后剩下的就是需要改变的字母. 最长不下降子序列:(我之前有写过,不懂请戳)http://blog.csdn.net/darwin_/article/details/38360997 POJ 3670: #include<cstdio> #include<cstring

poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式很简单:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2*f[i]-1. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> #include