Cod sursa(job #3242272)

Utilizator obsidianMidnight Majesty obsidian Data 10 septembrie 2024 19:07:49
Problema Potrivirea sirurilor Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 1.54 kb
//package strmatch;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    static final String INPUT_FILE = "strmatch.in";
    static final String OUTPUT_FILE = "strmatch.out";

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(INPUT_FILE));
        PrintWriter writer = new PrintWriter(OUTPUT_FILE);
        solve(reader, writer);
        reader.close();
        writer.flush();
        writer.close();
    }

    public static void solve(BufferedReader reader,
                             PrintWriter writer) throws IOException {
        String a = reader.readLine();
        String b = reader.readLine();
        StringMatchingStrategy strategy = new BruteForceStrategy();
        List<Integer> match = strategy.match(a, b);
        writer.println(match.size());
        for (int i = 0; i < Math.min(1000, match.size()); ++i) {
            writer.write(match.get(i) + " ");
        }
    }

    public interface StringMatchingStrategy {
        List<Integer> match(String a, String b);
    }

    public static class BruteForceStrategy implements StringMatchingStrategy {

        @Override
        public List<Integer> match(String a, String b) {
            List<Integer> result = new ArrayList<>();
            int it = b.indexOf(a);
            while (it != -1) {
                result.add(it);
                it = b.indexOf(a, it + 1);
            }
            return result;
        }
    }
}