Friday, December 25, 2009

Word Index ( 2009 )

This problem is from ProgFest Contest 2009( 02/14/2009 ). During the contest, I got stuck on this problem for 2 hours, really really dumb when I thought over it. It is probably one of the easiest problems that I have missed T_T !

Problem :

Consider the English alphabet {a,b,c,...z}. Using this alphabet, a set of valid words is 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. Note: This may not be a default-format. There is one line of output for each input line.

Sample Input
z
a
cat
vwxyz

Sample Output

26
1
0
83681

Solution :

#include <string>
#include <iostream>
#include <sstream>
#include <map>

using namespace std;

bool is_valid( const string& s ) {
for( int i = 1; i < s.length(); ++i ) {
if( s[ i ] <= s[ i - 1 ] )
return false;
}
return true;
}

map< string, int > generate() {
char previous_char;
int index = 1;
map< string, int > m;

int count = 0;
for( char i1 = 'a'; i1 <= 'z'; ++i1 ) {
count++;
ostringstream o;
o << i1;
m.insert( pair< string, int >( o.str(), count ) );
}

for( char i1 = 'a'; i1 <= 'z'; ++i1 ) {
for( char i2 = i1 + 1; i2 <= 'z'; ++i2 ) {
count++;
ostringstream o;
o << i1 << i2;
m.insert( pair< string, int >( o.str(), count ) );

}
}

for( char i1 = 'a'; i1 <= 'z'; ++i1 ) {
for( char i2 = i1 + 1; i2 <= 'z'; ++i2 ) {
for( char i3 = i2 + 1; i3 <= 'z'; ++i3 ) {
count++;
ostringstream o;
o << i1 << i2 << i3;
m.insert( pair< string, int >( o.str(), count ) );
}
}
}

for( char i1 = 'a'; i1 <= 'z'; ++i1 ) {
for( char i2 = i1 + 1; i2 <= 'z'; ++i2 ) {
for( char i3 = i2 + 1; i3 <= 'z'; ++i3 ) {
for( char i4 = i3 + 1; i4 <= 'z'; ++i4 ) {
count++;
ostringstream o;
o << i1 << i2 << i3 << i4;
m.insert( pair< string, int >( o.str(), count ) );
}
}
}
}

for( char i1 = 'a'; i1 <= 'z'; ++i1 ) {
for( char i2 = i1 + 1; i2 <= 'z'; ++i2 ) {
for( char i3 = i2 + 1; i3 <= 'z'; ++i3 ) {
for( char i4 = i3 + 1; i4 <= 'z'; ++i4 ) {
for( char i5 = i4 + 1; i5 <= 'z'; ++i5 ) {
count++;
ostringstream o;
o << i1 << i2 << i3 << i4 << i5;
m.insert( pair< string, int >( o.str(), count ) );
}
}
}
}
}

return m;
}

int main() {
map< string, int > m = generate();
string input;
while( cin >> input ) {
if( is_valid( input ) == false )
cout << "0" << endl;
else
cout << m[ input ] << endl;
}
return 0;
}


No comments:

Post a Comment