1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::io;

fn main() {	
	let mut input = String::new();
	io::stdin().read_line(&mut input).unwrap();
    let aa: Vec<u32> = input.trim().chars().map(|c| c.to_digit(10).unwrap()).collect();
	input = String::new();
	io::stdin().read_line(&mut input).unwrap();
    let bb: Vec<u32> = input.trim().chars().map(|c| c.to_digit(10).unwrap()).collect();
	input = String::new();
	io::stdin().read_line(&mut input).unwrap();
    let cc: Vec<u32> = input.trim().chars().map(|c| c.to_digit(10).unwrap()).collect();

    // println!("{:?}", aa); println!("{:?}", bb); println!("{:?}", cc);

	let n: usize = aa.len();

	let mut hh: Vec<char> = Vec::new();

	for i in 0..n {
		hh.push(match (aa[i] + bb[i]) as i32 - cc[i] as i32 {
			0 => 'Z',
			10 => 'P',
			-1 => 'R',
			9 => 'X',
			_ => 'N'
		});
	}

	let mut vv: Vec<i8> = Vec::new();
	for i in 0..n-1 {
		vv.push(matches!((hh[i], hh[i+1]), 
			('R','R') | ('R','Z') | ('Z','P') | ('Z','X') | 
			('X','R') | ('X','Z') | ('P','P') | ('P','X')) as i8);
	}
	vv.push(0);
	vv.push(0);
	hh.push('N');
	hh.push('N');

	let mut xx: Vec<i8> = Vec::new();
	for i in 0..n {
		xx.push(match hh[i] { 'Z'|'R' => 1, _ => 0 } );
	}
	let mut yy: Vec<i64> = Vec::new();
	let mut sy: Vec<i64> = Vec::new();
	sy.push(0);
	for i in 0..n {
		yy.push(match hh[i] { 'Z'|'P' => 1, _ => 0 } );
		sy.push(sy[i] + yy[i]);
	}

	// println!("hh {:?}", hh); println!("vv {:?}", vv); println!("xx {:?}", xx); println!("yy {:?}", yy);

	let mut d : usize = 0;
	let mut g : usize;
	
	let mut result : i64 = 0;
	
	while d < n {
		g = d;
		while hh[g] != 'N' && (g == d || vv[g-1] == 0) { g += 1; }
		
		// let m = (g-d) as i64; if m > 0 { println!("{:?}", (d, g, m)); }
		
		for x in d..g {
			if xx[x] == 0 { continue; }
			result += sy[g] - sy[x]; 
		}
				
		if hh[g] == 'N' || g==d { d = g+1; } else { d = g; }
	}
	
	println!("{:?}", result);
}