0% found this document useful (0 votes)
190 views2 pages

Code Jam 2009 Java

This document contains code for three Java classes - Alien, Water, and Welcome. The Alien class contains code to count the number of words in a dictionary that match a regular expression pattern. The Water class contains code to identify distinct basins in a 2D elevation map and label them with different characters. The Welcome class contains code to calculate the number of distinct subsequences that match a target string using dynamic programming.

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views2 pages

Code Jam 2009 Java

This document contains code for three Java classes - Alien, Water, and Welcome. The Alien class contains code to count the number of words in a dictionary that match a regular expression pattern. The Water class contains code to identify distinct basins in a 2D elevation map and label them with different characters. The Welcome class contains code to calculate the number of distinct subsequences that match a target string using dynamic programming.

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Archivo: /home/misan/gcj/2009/C1/Welcome.

java Página 1 de 2

import [Link].*;
import [Link].*;

class Alien {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int L = [Link]();
int D = [Link]();
String[] dictionary = new String[D];
int N = [Link](); [Link]();
for(int i=0; i<D; i++) {
dictionary[i]=[Link]();
}
for(int i=0; i<N; i++) {
int count = 0;
[Link]("Case #"+(i+1)+": ");
String pattern = [Link]();
pattern=[Link]("\\(","["); pattern=[Link]("\\)","]");
Pattern p = [Link](pattern);
for(String word : dictionary) {
Matcher m = [Link](word);
if([Link]()) count++; // [Link](pattern) alternative is slow!!
}
[Link](count);
}
}
}

/********************************************************************************************/

import [Link].*;

class Water {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int T = [Link]();
for(int i=0; i<T; i++) {
[Link]("Case #%d:\n",i+1);
int H = [Link]();
int W = [Link](); [Link]();
int[][] map = new int[W][H];
char[] out = new char[W*H];
int[] next = new int[W*H];
[Link](next,-1);
for(int j=0;j<H;j++) for(int k=0;k<W;k++) map[k][j]=[Link]();
char basin='a';
for(int y=0; y<H;y++) for( int x=0; x<W; x++ ) { // explore the cells ...
int min = map[x][y]; int x1=-1,y1=-1; int n=-1;
if(y-1>=0) if(map[x][y-1]<min) {min=map[x][y-1]; x1=x; y1=y-1; n=1;} // NORTH
if(x-1>=0) if(map[x-1][y]<min) {min=map[x-1][y]; x1=x-1; y1=y; n=2;} // WEST
if(x+1<W) if(map[x+1][y]<min) {min=map[x+1][y]; x1=x+1; y1=y; n=4;} // EAST
if(y+1<H) if(map[x][y+1]<min) {min=map[x][y+1]; x1=x; y1=y+1; n=3;} // SOUTH
if(n>-1) next[x+y*W]=x1+y1*W; //next cell candidate, if any ... (or it's a sink)
}
for(int j=0;j<W*H; j++) if(out[j]<'a') { // not yet marked
Stack<Integer> l = new Stack<Integer>();
int n = j;
do {[Link](n); n=next[n]; } while(n!=-1);
if(out[[Link]()]<'a') {for(int k : l) out[k]=basin; basin++;} // new basin, new color
else {char c=out[[Link]()]; for(int k : l) out[k]=c;} // existing basin, same color
}
for(int j=0;j<H;j++) {for(int k=0;k<W;k++) [Link](out[j*W+k]+" ");
[Link]();}
}
}
}

/********************************************************************************************/

import [Link].*;

class Welcome {
Archivo: /home/misan/gcj/2009/C1/[Link] Página 2 de 2

public static void main(String[] args) {


Scanner in = new Scanner([Link]);
int N = [Link](); [Link]();
for(int n=0; n<N; n++) {
String target = "welcome to code jam";
String s = [Link]();
long ans[][] = new long[[Link]()+1][[Link]()+1];
ans[0][0] = 1;
for( int i = 0; i < [Link](); i++) {
for( int j = 0; j < [Link](); j++ ){
if( [Link](j) == [Link](i)) {
ans[i+1][j+1] = (ans[i][j] + ans[i+1][j+1]) % 10000;
}
ans[i+1][j] = (ans[i][j] + ans[i+1][j]) % 10000;
}
}
long result = 0;
for( int i = 0; i <= [Link](); i++) {
result = (result + ans[i][[Link]()]) % 10000;
}
result += 10000;
String r = new String(); r="0000"+result;
[Link]("Case #%d: %s\n",n+1,[Link]([Link]()-4));
}
}
}

You might also like