MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 489 - Hangman Judge
AOAPC II

UVaOJ 489 - Hangman Judge

Chai
2021-11-24 0:00:00在线评测阅读 257

问题描述

p489

原题链接:UVaOJ 489 - Hangman Judge

相关说明:本题为《算法竞赛入门经典(第2版)》例题 4-2

解法一:模拟 + 集合

C++
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
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  int round;
  string sol, seq;
  while (cin >> round && round != -1) {
    cout << "Round " + to_string(round) << endl;
    cin >> sol, cin >> seq;
    int stroke = 0, status = 0;
    set<char> letter;
    for (auto c : sol) letter.insert(c);
    for (auto c : seq) {
      if (letter.find(c) != letter.end()) {
        letter.erase(c);
        if (letter.empty()) {
          cout << "You win." << endl;
          status = 1;
          break;
        }
      } else {
        stroke += 1;
        if (stroke == 7) {
          cout << "You lose." << endl;
          status = 2;
          break;
        }
      }
    }
    if (!status) cout << "You chickened out." << endl;
  }
  return 0;
}
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while True:
    round_num = input()
    if int(round) == -1:
        break
    print("Round", round_num)
    sol, seq = input(), input()
    letter, stroke, status = set(), 0, 0
    for c in sol:
        letter.add(c)
    for c in seq:
        if c in letter:
            letter.remove(c)
            if len(letter) == 0:
                print("You win.")
                status = 1
                break
        else:
            stroke += 1
            if stroke == 7:
                print("You lose.")
                status = 2
                break
    if not status:
        print("You chickened out.")
AOAPC II UVaOJ 字符串
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz