Wednesday, February 8, 2012

Google Code Jam Practice

I've never participated in Google Code Jam before, but I decided to participate in the contest this year.

It will be held in March , so I have about a month to prepare.

and read explanations about the contest, and clicked "Practice and Learn."

I started with the easiest problem , as google suggested.

The problem I solved was called "Reverse String." Link :

All I had to do was
1. Receive a string.
2. Reverse the order of the word (but keep the order of each word. That is, do not turn abcd to dcba.)

For example, given input "abcd def ghi"
output "ghi def abcd"

The algorithm itself was not hard. However, it was difficult for me to read and write to files because I have little experience in it.

I remember learning file input/output in shell (Linux) environment, so I assumed it would be possible to use the commands to give inputs to and receive outputs from my program. It worked.

This is how I solved Reverse String.

Assume you are at directory

The current directory has two files
1. MyProgram.cpp
2. input.txt

Run following commands in your shell
#1  g++ MyProgram.cpp -o MyProgram.out   
 ===> compiles MyProgram.cpp file and make an executable file named "MyProgram.out"

#2 cat input.txt | ./MyProgram.out > result.txt
command "cat input.txt" outputs all contents in input.txt to standard output.
"| ./MyProgram.out" runs MyProgram.out using data from the standard output.
> result.txt stores all output from MyProgram.out to result.txt

And below is my source code for "ReverseString"

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

#define REP(i,a,b) for(int i=a; i < b; i++)
#define REPE(i, a, b) for(int i=a; i <=b; i++)
int INF = numeric_limits<int>::max();
int nINF = numeric_limits<int>::min();
typedef long long ll;

int main()
int N;

for(int i=0; i < N; i++)
string str;
getline(cin, str);
stringstream ss;
string word;
string ans = "";
ans = word + " " + ans;
ans.erase(ans.size() - 1);
cout<<"Case #"<<(i+1)<<": "<<ans<<endl;
    return 0;

No comments:

Post a Comment