Submission #5354977


Source Code Expand

Copy
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

struct UnionFind {
    parent: Vec<usize>,
    rank: Vec<usize>,
    size: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let mut p = vec![0; n];
        for i in 0..n {
            p[i] = i;
        }
        return UnionFind {
            parent: p,
            rank: vec![0; n],
            size: vec![1; n],
        };
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.parent[x] {
            x
        } else {
            let p = self.parent[x];
            let pr = self.find(p);
            self.parent[x] = pr;
            pr
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    fn unite(&mut self, a: usize, b: usize) {
        let a_root = self.find(a);
        let b_root = self.find(b);
        if self.rank[a_root] > self.rank[b_root] {
            self.parent[b_root] = a_root;
            self.size[a_root] += self.size[b_root];
        } else {
            self.parent[a_root] = b_root;
            self.size[b_root] += self.size[a_root];
            if self.rank[a_root] == self.rank[b_root] {
                self.rank[b_root] += 1;
            }
        }
    }

    fn get_size(&mut self, x: usize) -> usize {
        let root = self.find(x);
        self.size[root]
    }
}

fn main() {
    input! {
        n: usize,
        ss: [chars; n],
    }
    let mut ans = 0;
    let mut x = 0;
    let mut y = 0;
    let mut z = 0;
    for s in ss.iter() {
        for i in 0..s.len() - 1 {
            if s[i] == 'A' && s[i + 1] == 'B' {
                ans += 1;
            }
        }
        if s[0] == 'B' && s[s.len() - 1] == 'A' {
            y += 1;
        } else if s[0] == 'B' {
            z += 1;
        } else if s[s.len() - 1] == 'A' {
            x += 1;
        }
    }
    if x * z == 0 && y > 0 {
        ans += y - 1;
    } else {
        ans += std::cmp::min(x, z) + y;
    }
    println!("{}", ans);
}

Submission Info

Submission Time
Task C - AB Substrings
User sly
Language Rust (1.15.1)
Score 0
Code Size 3792 Byte
Status WA
Exec Time 5 ms
Memory 4352 KB

Compile Error

warning: struct is never used: `UnionFind`, #[warn(dead_code)] on by default
  --> ./Main.rs:66:1
   |
66 |   struct UnionFind {
   |  _^ starting here...
67 | |     parent: Vec<usize>,
68 | |     rank: Vec<usize>,
69 | |     size: Vec<usize>,
70 | | }
   | |_^ ...ending here

warning: method is never used: `new`, #[warn(dead_code)] on by default
  --> ./Main.rs:73:5
   |
73 |     fn new(n: usize) -> UnionFind {
   |     ^

warning: method is never used: `find`, #[warn(dead_code)] on by default
  --> ./Main.rs:85:5
   |
85 |     fn find(&mut self, x: usize) -> usize {
   |     ^

warning: method is never used: `same`, #[warn(dead_code)] on by default
  --> ./Main.rs:96:5
   |
96 |       fn same(&mut self, a: usize, b: usize) -> bool {
   |  _____^ starting here...
97 | |         self.find(a) == self.find(b)
98 | |     }
   | |_____^ ...ending here

warning: method is never used: `unite`, #[warn(dead_code)] on by default
   --> ./Main.rs:100:5
    |
100 |     fn unite(&mut self, a: usize, b: usize) {...

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 2
WA × 1
AC × 36
WA × 12
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All rand_01.txt, rand_02.txt, rand_03.txt, rand_04.txt, rand_05.txt, rand_06.txt, rand_07.txt, rand_08.txt, rand_09.txt, rand_10.txt, rand_11.txt, rand_12.txt, rand_13.txt, rand_14.txt, rand_15.txt, rand_16.txt, rand_17.txt, rand_18.txt, rand_19.txt, rand_20.txt, rand_21.txt, rand_22.txt, rand_23.txt, rand_24.txt, rand_25.txt, rand_26.txt, rand_27.txt, rand_28.txt, rand_29.txt, rand_30.txt, rand_31.txt, rand_32.txt, rand_33.txt, rand_34.txt, rand_35.txt, rand_36.txt, rand_37.txt, rand_38.txt, rand_39.txt, rand_40.txt, rand_41.txt, rand_42.txt, rand_43.txt, rand_44.txt, rand_45.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
rand_01.txt AC 5 ms 4352 KB
rand_02.txt AC 5 ms 4352 KB
rand_03.txt AC 5 ms 4352 KB
rand_04.txt AC 5 ms 4352 KB
rand_05.txt AC 5 ms 4352 KB
rand_06.txt AC 5 ms 4352 KB
rand_07.txt AC 5 ms 4352 KB
rand_08.txt AC 5 ms 4352 KB
rand_09.txt AC 4 ms 4352 KB
rand_10.txt AC 4 ms 4352 KB
rand_11.txt AC 2 ms 4352 KB
rand_12.txt AC 3 ms 4352 KB
rand_13.txt AC 5 ms 4352 KB
rand_14.txt AC 4 ms 4352 KB
rand_15.txt AC 4 ms 4352 KB
rand_16.txt AC 4 ms 4352 KB
rand_17.txt WA 2 ms 4352 KB
rand_18.txt WA 3 ms 4352 KB
rand_19.txt WA 2 ms 4352 KB
rand_20.txt WA 3 ms 4352 KB
rand_21.txt AC 5 ms 4352 KB
rand_22.txt AC 5 ms 4352 KB
rand_23.txt AC 5 ms 4352 KB
rand_24.txt AC 5 ms 4352 KB
rand_25.txt AC 5 ms 4352 KB
rand_26.txt AC 5 ms 4352 KB
rand_27.txt WA 4 ms 4352 KB
rand_28.txt WA 4 ms 4352 KB
rand_29.txt WA 4 ms 4352 KB
rand_30.txt AC 4 ms 4352 KB
rand_31.txt AC 3 ms 4352 KB
rand_32.txt AC 4 ms 4352 KB
rand_33.txt AC 4 ms 4352 KB
rand_34.txt WA 3 ms 4352 KB
rand_35.txt WA 4 ms 4352 KB
rand_36.txt AC 3 ms 4352 KB
rand_37.txt AC 3 ms 4352 KB
rand_38.txt AC 4 ms 4352 KB
rand_39.txt WA 4 ms 4352 KB
rand_40.txt AC 4 ms 4352 KB
rand_41.txt AC 4 ms 4352 KB
rand_42.txt AC 3 ms 4352 KB
rand_43.txt AC 4 ms 4352 KB
rand_44.txt WA 4 ms 4352 KB
rand_45.txt AC 4 ms 4352 KB
sample_01.txt AC 2 ms 4352 KB
sample_02.txt AC 2 ms 4352 KB
sample_03.txt WA 2 ms 4352 KB