Cod sursa(job #3301909)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 1 iulie 2025 03:11:28
Problema Abc2 Scor 0
Compilator rs Status done
Runda Arhiva de probleme Marime 1.58 kb
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;
use std::io::{self, BufRead};

fn main() {
    let input = File::open("abc2.in").expect("Unable to open file");
    let mut out = File::create("abc2.out").expect("Unable to create file");

    let mut reader = io::BufReader::new(input);
    let mut sir = String::new();
    reader.read_line(&mut sir).expect("Unable to read line");

    let mut words = HashSet::new();
    let mut word_size = 0;

    for line_result in reader.lines() {
        match line_result {
            Ok(line) => {
                let mut num: u64 = 0;
                word_size = line.len();
                for c in line.chars() {
                    let digit = match c {
                        'a' => 0,
                        'b' => 1,
                        'c' => 2,
                        _ => panic!("Unexpected character in input"),
                    };
                    num = num * 3 + digit;
                }
                words.insert(num);
            }
            Err(_) => break,
        }
    }

    let mut number = 0;
    let mut result = 0;
    let modulo = 3_u64.pow(word_size as u32);

    for c in sir.trim().chars() {
        let digit = match c {
            'a' => 0,
            'b' => 1,
            'c' => 2,
            _ => panic!("Unexpected character in input"),
        };
        number = (number * 3 + digit) % modulo;
        if words.contains(&number) {
            result += 1;
        }
        // println!("digit={} result={}", digit, result);
    }

    writeln!(out, "{}", result).expect("Unable to write to file");
}