Friday, March 23, 2012

Problem 108: Your Ride Is Here

This one is also easy! It's too bad I failed a few times due to const-correctness.

You pretty much "hash" each string, as a product of letters, and compare both hashes mod 47. If they match, "GO", otherwise "STAY" and then print a newline.

On second thought, I could have placed the modulus in the s_product function. Then I also realized that it could be put in after each multiplication operation.
/*
ID: victor4
PROG: ride
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int s_product(const char *s){
 int product = 1;
 for(const char *p = s; *p; ++p)
  product *= *p - 'A' + 1;
 return product;
}

int main() {
 ofstream fout ("ride.out");
 ifstream fin ("ride.in");
 
 string s_in1, s_in2;
 fin >> s_in1 >> s_in2;
 
 fout << (((s_product(s_in1.c_str()) % 47) == (s_product(s_in2.c_str()) % 47)) ? "GO" : "STAY") << endl;
 
 return 0;
}

No comments:

Post a Comment