Cod sursa(job #3313216)

Utilizator game_difficultyCalin Crangus game_difficulty Data 2 octombrie 2025 21:11:54
Problema Potrivirea sirurilor Scor 14
Compilator rs Status done
Runda Arhiva educationala Marime 1.16 kb
use std::fs::File;
#[allow(unused_imports)]
use std::io::{BufRead, BufReader, Read, Write};
use std::{cmp::min, error::Error};

const FILE: &'static str = "strmatch";

fn main() -> Result<(), Box<dyn Error>> {
    let mut fin = BufReader::new(File::open(format!("{}.in", FILE)).unwrap());
    let mut fout = File::create(format!("{}.out", FILE)).unwrap();

    let mut a = String::new();
    let mut b = String::new();

    fin.read_line(&mut a)?;
    fin.read_line(&mut b)?;

    let a = a.trim().as_bytes();
    let b = b.trim().as_bytes();

    if a.len() > b.len() {
        write!(&mut fout, "0")?;
        return Ok(());
    }

    let mut ans = vec![];
    let mut nextpos = 0;
    let mut i = 0;
    while i < b.len() {
        if nextpos < a.len() {
            if a[nextpos] == b[i] {
                nextpos += 1;
            } else {
                nextpos = 0;
            }
            i += 1;
        } else {
            ans.push(i - a.len());
            nextpos = 0;
            i = i - a.len() + 1;
        }
    }

    writeln!(&mut fout, "{}", ans.len())?;
    for &x in &ans[0..(min(ans.len(), 1000))] {
        write!(&mut fout, "{} ", x)?;
    }

    Ok(())
}