Google code Jam : i am not a contest programmer  

Posted by Unknown in ,

I may be among the best programmers of my batch in my college but definitely I am not worth calling myself as a programmer. I am far from a standard contest programmer for my programs are still those written on college papers. Plus I am still new to the STl and the contest world. i have just had a few outings. But I plan to have massive changes to this state this year. I have started with the google code jam, a big programming contest popularised by google for programmer's all over the world. The contest had its qualifiers on thursday and indeed as said qualifiers are for only to sort out people who don't know what programming is, it came out. The first question was sort of easy and so was the second one( Even i could do that). But the third one was gud. Just not really as an article I write this posting to my blog to help me trace if i can improve as a programmaer this year. You can expect to see top coder contest codes to be here as well, for I just joined that.
Here is the link to the files Ques+Mine as well as ideal answers + Hope I see this next year:http://w14.easy-share.com/1700957331.html
I have converted the cpp to html. I have used a project at codeproject. The files are at:http://w14.easy-share.com/1700966549.html
So now I can paste the stuff here:
Saving the universe
Problem

The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding.

The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode!

To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they're received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.

Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.

Input

The first line of the input file contains the number of cases, N. N test cases follow.

Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.

The following line contains a number Q -- the number of incoming queries. The next Q lines will each contain a query. Each query will be the name of a search engine in the case.

Output

For each input case, you should output:
Case #X: Y
where X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.


Limits

0 <>Input
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol

Output
Case #1: 1
Case #2: 0

So here is what I managed: Not good but i need to keep a track of myself.



// savetheuniverse.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"
#include<iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int no =0;
cin>>no;
for(int counter = 1; counter<=no;counter++)
{
printf("Case #%d: ",counter);
int NoOfEngines = 0;
cin>>NoOfEngines;
char useless[7];
if(NoOfEngines != 0 )
gets_s(useless);
//cout<<NoOfEngines<<endl;
char Eng[1000][100];
int CalledEng[1000] = {0};
for (int Engine = 0;Engine<NoOfEngines;Engine++)
{
gets_s(Eng[Engine]);
//cout<<Eng[Engine]<<endl;
}
int NoOfSearches;
cin>>NoOfSearches;
//cout<<NoOfSearches<<endl;
if(NoOfSearches != 0 )
gets_s(useless);
char Sear[1000][100];
for(int Search = 0;Search <NoOfSearches; Search++)
{
gets_s(Sear[Search]);
//cout<<Sear[Search]<<endl;
}

int Present = 1;
int LastElementIndex=0;
while(1)
{

for(int i=LastElementIndex;i<NoOfSearches;i++)
{
for(int j=0;j<NoOfEngines;j++)
{
if(strcmp(Sear[i],Eng[j]) == 0)
{

if(CalledEng[j] != Present)
{
CalledEng[j] =Present;
LastElementIndex = i;
}
break;
}
}
}
//if(counter==8)
//printf("%d\t%d\n",Present,LastElementIndex);
int flag=0;
for(int i=0;i<NoOfEngines;i++)
{
if(CalledEng[i]<Present) {flag =1 ;}
}
if(flag==1) {break;}
Present++;
if(LastElementIndex >= NoOfSearches-1) break;
}
printf("%d\n",Present-1);
}
return 0;
}



And this was the ideal answer

#include <map>

#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;

#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()

template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}

template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }

char buf[1024*1024];

int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);

gets(buf);
For(test, 1, atoi(buf)) {
int s, q;
map<string,int> num;
gets(buf);
s = atoi(buf);
Rep(i, s) {
gets(buf);
num[buf] = i;
}
gets(buf);
q = atoi(buf);
vi dp(s, 0);
Rep(i, q) {
gets(buf);
assert(num.count(buf) > 0);
int id = num[buf];
Rep(j, s)
if (j != id)
checkMin(dp[j], dp[id]+1);
dp[id] = 1000000;
}
printf("Case #%d: %d\n", test, *min_element(All(dp)));
}

exit(0);
}

I'll write answer 2 and 3 in a seperate post

This entry was posted on Sunday, July 20, 2008 at Sunday, July 20, 2008 and is filed under , . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment