Cod sursa(job #3242276)

Utilizator obsidianMidnight Majesty obsidian Data 10 septembrie 2024 19:26:11
Problema Potrivirea sirurilor Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 1.82 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();
    }

    private static final int LIMIT = 1000;

    public static void solve(BufferedReader reader,
                             PrintWriter writer) throws IOException {
        String a = reader.readLine();
        String b = reader.readLine();
        StringMatchingStrategy strategy = new BruteForceStrategy();
        MatchingResult match = strategy.match(a, b, LIMIT);
        writer.println(match.count);
        for (int i = 0; i < match.idx.size(); ++i) {
            writer.write(match.idx.get(i) + " ");
        }
    }

    public record MatchingResult(int count, List<Integer> idx) {
    }

    public interface StringMatchingStrategy {
        MatchingResult match(String a, String b, int limit);
    }

    public static class BruteForceStrategy implements StringMatchingStrategy {

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