我最近尝试在open.kattis.com上进行在线编程挑战Texture Analysis;但是,尽管我在个人测试中对示例案例的回答是正确的,但我仍然弄错了第二个示例案例,提交状态为:“错误答案”。我的代码在做什么错?
Link to the kattis problem。我的代码:
use std::io::{self, BufRead};
fn main() {
let mut line: i8 = 0;
let stdin = io::stdin();
for input in stdin.lock().lines().map(|l| l.unwrap()) {
line += 1;
if input == "END" {
break;
}
let pattern = input
.split("*")
.filter(|&i| i.contains("."))
.collect::<Vec<&str>>();
let c = pattern
.iter()
.all(|j| j.len() == pattern[0].len());
if (c && pattern.len() != 1 ) || pattern.len() == 0 {
println!("{} EVEN", line);
} else {
println!("{} NOT EVEN", line);
}
}
}
最佳答案
固定评论示例:
use std::io::{self, BufRead};
fn main() {
// line needs to store values up to 2000
// it cannot be an i8, must be an u16 at minimum
// Rust's default for integer literals is i32 which we use here
let mut line = 0;
let stdin = io::stdin();
for input in stdin.lock().lines().map(|l| l.unwrap()) {
line += 1;
if input == "END" {
break;
}
// not possible to construct NON-EVEN input in <= 3 chars
if input.len() <= 3 {
println!("{} EVEN", line);
continue;
}
// ignore first & last * when splitting input
let pattern = &input[1..input.len()-1]
.split("*")
// do not filter out empty strings
.collect::<Vec<&str>>();
let space_len = pattern[0].len();
let is_even = pattern
.iter()
.all(|j| j.len() == space_len);
if is_even {
println!("{} EVEN", line);
} else {
println!("{} NOT EVEN", line);
}
}
}
关于loops - 关于kattis问题: texture analysis的错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65347795/