Informatika gyűjtemény

Egy szinttel feljebb fg_welcome3.java

2004050607080910

NézetNyomtat

fg_welcome3.java (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 2 KB
package welcometocodejamszk;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class fg_welcome3 {

    public static void main(String[] args) throws IOException {
        new fg_welcome3().run();
    }
    
    String sub, main;
    int accel[][];
    
    // accel[i][j] = count(i, j) = 
    //       a sub[i...] string előfordulásainak a száma a main[j...] stringben
        
    
    void count(int subPos, int mainPos) {
        int res = 0;
        
        if (subPos == sub.length()) {
            res = 1;
        } else if (mainPos == main.length()) {
            res = 0;
        } else {
            if (mainPos < main.length() - 1) {
                res = res + accel[subPos][mainPos+1];
            }
            if (main.charAt(mainPos) == sub.charAt(subPos)) {
                res = res + accel[subPos+1][mainPos+1];
            }
        }
        
        accel[subPos][mainPos] = res % 10000;
    }
    
    void doubleFill(int[][] x, int val) {
        for (int j = 0; j < x.length; j++) {
            Arrays.fill(x[j], -1);
        }
    }
    
    int solve() {
        accel = new int[sub.length()+1][main.length()+1];
        doubleFill(accel, -1);
        for (int i = main.length(); i >= 0; i--) {
            for (int j = sub.length(); j >= 0; j--) {
                count(j, i);
            }
        }
        return accel[0][0];
    }
    
    void run() throws IOException {
        //String code = "small-attempt0";
        //String code = "0";
        //String code = "large";
        String code = "szakkor";
        BufferedReader br = new BufferedReader(new FileReader("C-"+code+".in"));
        PrintWriter pw = new PrintWriter(new FileWriter("C-"+code+".out"));
        sub = br.readLine();
        int N = Integer.parseInt(br.readLine());
        for (int i = 1; i <= N; i++) {
            main = br.readLine();
            int res = solve(); //itt szamoljuk ki az eredmenyt
            pw.printf("Case #%1$d: %2$04d\n", i, res); //eredmeny kiirasa
        }
        br.close();
        pw.close();
    }

}
(Vissza)