POJ - 1850 Code
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
考虑过纯暴力,但是肯定会超时(我为什么这么喜欢纯暴力呢,真让人神伤),然后……当时就没有然后了,补题的时候才发现其实这次挂的是排列组合,这次没做好真的不能把所有的锅都甩到搜索上,因为不是所有的题都是搜索,比如这个……其实真的没有什么高深的算法,但是就是不会,也是做得少的原因吧。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#define N 200100
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAX=2e6+5;
double eps=0.000001;
long long n,m;
char tm[15];
long long cc[100][100];
void pre(){
long long i,j;
for(i=0;i<=40;i++){
cc[i][0]=1;
}
for(i=1;i<=40;i++){
for(j=1;j<=40;j++){
cc[i][j]=(cc[i-1][j-1]+cc[i-1][j]);
}
}
}
int check(char *s,int len){
for(int i=1;i<len;i++){
if(s[i]<s[i-1]){
return 0;
}
}
return 1;
}
int main(int argc, char** argv)
{
int len,ret,tmp;
long long sum,i,j;
sum=0;
pre();
scanf("%s",tm);
len=strlen(tm);
ret=check(tm,len);
if(ret==0){
printf("0\n");
return 0;
}
for(i=1;i<len;i++){
sum+=cc[26][i];
}
if(tm[0]>'a'){
tmp=0;
for(i=1;i<=tm[0]-'a';i++){
tmp+=cc[26-i][len-1];
}
sum+=tmp;
}
for(i=1;i<len;i++){
if(tm[i]-1-tm[i-1]>0){
tmp=0;
for(j=tm[i-1]-'a'+2;j<=tm[i]-'a';j++){
tmp+=cc[26-j][len-i-1];
}
sum+=tmp;
}
}
printf("%lld\n",sum+1);
return 0;
}