Code Jam 2009 Java
Code Jam 2009 Java
java Página 1 de 2
import java.util.*;
import java.util.regex.*;
class Alien {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int L = in.nextInt();
int D = in.nextInt();
String[] dictionary = new String[D];
int N = in.nextInt(); in.nextLine();
for(int i=0; i<D; i++) {
dictionary[i]=in.nextLine();
}
for(int i=0; i<N; i++) {
int count = 0;
System.out.print("Case #"+(i+1)+": ");
String pattern = in.nextLine();
pattern=pattern.replaceAll("\\(","["); pattern=pattern.replaceAll("\\)","]");
Pattern p = Pattern.compile(pattern);
for(String word : dictionary) {
Matcher m = p.matcher(word);
if(m.matches()) count++; // word.matches(pattern) alternative is slow!!
}
System.out.println(count);
}
}
}
/********************************************************************************************/
import java.util.*;
class Water {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for(int i=0; i<T; i++) {
System.out.printf("Case #%d:\n",i+1);
int H = in.nextInt();
int W = in.nextInt(); in.nextLine();
int[][] map = new int[W][H];
char[] out = new char[W*H];
int[] next = new int[W*H];
Arrays.fill(next,-1);
for(int j=0;j<H;j++) for(int k=0;k<W;k++) map[k][j]=in.nextInt();
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 {l.push(n); n=next[n]; } while(n!=-1);
if(out[l.peek()]<'a') {for(int k : l) out[k]=basin; basin++;} // new basin, new color
else {char c=out[l.peek()]; 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++) System.out.print(out[j*W+k]+" ");
System.out.println();}
}
}
}
/********************************************************************************************/
import java.util.*;
class Welcome {
Archivo: /home/misan/gcj/2009/C1/Welcome.java Página 2 de 2