Cod sursa(job #3313218)

Utilizator game_difficultyCalin Crangus game_difficulty Data 2 octombrie 2025 21:22:23
Problema Potrivirea sirurilor Scor 80
Compilator rs Status done
Runda Arhiva educationala Marime 1.42 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 debugfout = File::create("debug.out").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();
    //writeln!(&mut debugfout, "len: {}", a.len())?;
    let b = b.trim().as_bytes();

    let mut ans = vec![];
    let mut nextpos = 0;
    let mut i = 0;
    while i < b.len() {
        if nextpos < a.len() {
            //writeln!(&mut debugfout, "i: {}, np: {}, a: {}, b: {}", i, nextpos, a[nextpos], b[i])?;
            if a[nextpos] == b[i] {
                nextpos += 1;
            } else {
                i = i + 1 - nextpos;
                nextpos = 0;
                continue;
            }
            if nextpos < a.len() {
                i += 1;
            } else {
                ans.push(i + 1 - a.len());
                nextpos = 0;
                i = i + 2 - a.len();
            }
        }
    }

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

    Ok(())
}