Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

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

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

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int N = in.nextInt(); in.nextLine();
for(int n=0; n<N; n++) {
String target = "welcome to code jam";
String s = in.nextLine();
long ans[][] = new long[s.length()+1][target.length()+1];
ans[0][0] = 1;
for( int i = 0; i < s.length(); i++) {
for( int j = 0; j < target.length(); j++ ){
if( target.charAt(j) == s.charAt(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 <= s.length(); i++) {
result = (result + ans[i][target.length()]) % 10000;
}
result += 10000;
String r = new String(); r="0000"+result;
System.out.printf("Case #%d: %s\n",n+1,r.substring(r.length()-4));
}
}
}

You might also like