complete 4674.rs 9912.rs 10826.rs 12140.rs 14452.rs 17021.rs 17386.rs 20929.rs
This commit is contained in:
51
storage/zeta/rs/completed/4674.rs
Normal file
51
storage/zeta/rs/completed/4674.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::io::stdin;
|
||||
|
||||
fn convert(text: Vec<char>) -> Vec<u64> {
|
||||
text.into_iter()
|
||||
.map(|x| match x {
|
||||
'_' => 0,
|
||||
'a'..='z' => (x as u8 - 'a' as u8 + 1) as u64,
|
||||
'.' => 27,
|
||||
_ => u64::MAX,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn revert(code: Vec<u64>) -> Vec<char> {
|
||||
code.into_iter()
|
||||
.map(|x| match x {
|
||||
0 => '_',
|
||||
1..=26 => (b'a' + x as u8 - 1) as char,
|
||||
27 => '.',
|
||||
_ => '*',
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn decrypt(k: u64, ciphertext: Vec<char>) -> Vec<char> {
|
||||
let n = ciphertext.len();
|
||||
let ciphercode = convert(ciphertext);
|
||||
let mut plaincode = vec![0u64; n];
|
||||
|
||||
for i in 0..n {
|
||||
let j = ((k * i as u64) % n as u64) as usize;
|
||||
plaincode[j] = ((ciphercode[i] as u64 + i as u64) % 28) as u64;
|
||||
}
|
||||
revert(plaincode)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
line.clear();
|
||||
stdin().read_line(&mut line).unwrap();
|
||||
if line.starts_with("0") {
|
||||
break;
|
||||
}
|
||||
let mut iter = line.split(' ');
|
||||
let k = iter.next().unwrap().trim().parse::<u64>().unwrap();
|
||||
let msg = iter.next().unwrap().trim().chars().collect();
|
||||
println!("{}", decrypt(k, msg).into_iter().collect::<String>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user