//Google Code Jam 2009, Qualification Round C Welcome to Code Jam
import java.awt.Frame;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Google Code Jam
public class GoogleCode1 {
private static MyScanner in;
private static PrintStream out;
private static void solve() throws IOException
{
int C = in.nextInt();
in.nextLine();
for (int i = 0; i < C; i++) {
out.print("Case #" + (i + 1) + ": ");
solveCase();
}
}
static String swcj = "welcome to code jam";
static char[] wcj;
private static void solveCase() throws IOException {
wcj = swcj.toCharArray();
String stext = in.nextLine();
char[] cstext = stext.toCharArray();
int wlen = wcj.length;
int slen = cstext.length;
int[][] f = new int[wlen][slen];
for (int c = 0; c < wlen; c++) {
for (int d = 0; d < slen; d++) {
int left = 0;
int above = 0;
if (c > 0) {
left = f[c - 1][d];
}
if (d > 0) {
above = f[c][d - 1];
}
if (wcj[c] == cstext[d]) {
if (c > 0)
f[c][d] = left + above;
else
f[c][d] = left + above + 1;
}
else {
f[c][d] = above;
}
f[c][d] = f[c][d] % 10000;
}
}
int cnt = f[wlen - 1][slen - 1];
String result = String.format("%04d", cnt % 10000);
out.println(result);
}
public static void main(String[] args) throws IOException { // helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.txt");
}
else {
in = new MyScanner();
out = System.out;
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
No comments:
Post a Comment