Word Index
Encoding schemes are often used in situations requiring encryption or
information storage/transmission
economy. Here, we develop a simple encoding scheme that encodes particular
types of words with five or fewer (lower case) letters as integers.
| Word Index |
Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,
abc aep gwz
are all valid three-letter words, whereas
aab are cat
are not.
For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:
a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681
Your program is to read a series of input lines. Each input line will have
a single word on it, that will be
from one to five letters long. For each word read, if the word is invalid
give the number 0. If the word read
is valid, give the word's position index in the above alphabetical list.
Input
The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line.The input will be terminated by end-of-file.
Output
The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.Sample Input
z a cat vwxyz
Sample Output
26 1 0 83681
Les dejo una solucion a este problema con una dinamica:
ResponderEliminar#include<stdio.h>
#include<string.h>
using namespace std;
char cd[7];
long a[7],i,j,k,s,l;
struct palabra{
long t; //aqui guardare la cantidad de palabras de tamaño i que empiezan con el caracter j
long m;//cantidad de palabras de tamaño i que empiezan con el caracter j o uno mas grande
} p[8][28];
int main(){
for(j=1;j<27;j++){
p[1][j].t=1;
p[1][j].m=26-j+1;
}
for(i=2;i<6;i++){
for(j=1;j<27;j++)
p[i][j].t=p[i-1][j+1].m;
for(j=26;j>0;j--)
p[i][j].m=p[i][j+1].m+p[i][j].t;
}
while(gets(cd)){
l=strlen(cd);
for(i=0;i<l;i++){
a[i]=cd[i]-'a'+1;
}
for(i=l;i<5;i++)
a[i]=100+i;
if(a[0]<a[1]&&a[1]<a[2]&&a[2]<a[3]&&a[3]<a[4]){
s=0;
for(i=1;i<l;i++){
s+=p[i][1].m;
s+=p[l-i][a[i-1]+1].m-p[l-i][a[i]].m;
}
s+=1+p[l][1].m-p[l][a[0]].m;
printf("%ld\n",s);
}else{
printf("0\n");
}
}
}
La Formula cerrada que encontre para calcular el problema se basa basicamente en calcular los p[i][j].m, que valen:
ResponderEliminar(26-i-j+2)(26-i-j+3)...(26-j+1)/i!