Skip to content

Commit

Permalink
e100: Add rust impl of length of continous letters
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 19, 2024
1 parent 8cbf107 commit 3b8f47b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/od/2024e100/length-of-continous-letters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@

```python
{{#include solution.py:6:}}
```

### Rust

```rust
{{#include src/main.rs:5:}}
```
65 changes: 64 additions & 1 deletion src/od/2024e100/length-of-continous-letters/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use std::cmp::Reverse;
use std::collections::HashMap;
use std::io::{stdin, BufRead};

fn main() {
println!("Hello, world!");
// 读取输入
let mut s = String::new();
let ret = stdin().lock().read_line(&mut s);
assert!(ret.is_ok());
let mut k_str = String::new();
let ret = stdin().lock().read_line(&mut k_str);
assert!(ret.is_ok());
let k: usize = k_str.trim().parse().unwrap();

// 先遍历字符串, 分隔出连续相同字符, 然后统计其个数, 存放到计数字典中
let mut current_char: char = 'A';
let mut current_char_count: usize = 0;
let mut char_dict: HashMap<char, usize> = HashMap::new();
for chr in s.trim().chars() {
// 如果当前的字符与上个字符不相同
if current_char != chr {
// 保存到字典中
if current_char_count > 0 {
// 如果该字符在字典中已经存在, 则只保存最大连续数
if let Some(last_count) = char_dict.get_mut(&current_char) {
*last_count = current_char_count.max(*last_count);
} else {
char_dict.insert(current_char, current_char_count);
}
}

// 重置上个字符及其计数
current_char = chr;
current_char_count = 1;
} else {
current_char_count += 1;
}
}

// 处理最后一个字符
if current_char_count > 0 {
// 如果该字符在字典中已经存在, 则只保存最大连续数
if let Some(last_count) = char_dict.get_mut(&current_char) {
*last_count = current_char_count.max(*last_count);
} else {
char_dict.insert(current_char, current_char_count);
}
}

// 将字典转换成列表
let mut word_list: Vec<(char, usize)> = char_dict.into_iter().collect();
// 基于最大连续数进行排序, 从高到低
word_list.sort_by_key(|pair| Reverse(pair.1));
//println!("{word_list:?}");

// 并找到第 k 个字符, 注意下标从0开始计数, 而k是从1开始的
if k <= word_list.len() {
println!("{}", word_list[k - 1].1);
} else {
println!("-1");
}
}

0 comments on commit 3b8f47b

Please sign in to comment.