Informatika gyűjtemény

Egy szinttel feljebb fg_welcome1.java

2004050607080910

NézetNyomtat

fg_welcome1.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: 1 KB
package welcometocodejamszk;

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

public class fg_welcome1 {

    public static void main(String[] args) throws IOException {
        new fg_welcome1().run();
    }
    
    String sub, main;
    
    //
    // count(i, j) = a sub[i...] string előfordulásainak a száma a main[j...] stringben
    //
    
    int 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 + count(subPos, mainPos+1);
            }
            if (main.charAt(mainPos) == sub.charAt(subPos)) {
                res = res + count(subPos+1, mainPos+1);
            }
        }
        
        
        return res % 10000;
    }
    
    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 = count(0, 0); //itt szamoljuk ki az eredmenyt
            pw.printf("Case #%1$d: %2$04d\n", i, res); //eredmeny kiirasa
        }
        br.close();
        pw.close();
    }

}
(Vissza)