leetcode-master/problems/kamacoder/0133.小红买药.md

49 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 133. 小红买药
[题目链接](https://kamacoder.com/problempage.php?pid=1210)
本题是一道直观的模拟题,但也并不简单,很多情况容易漏了,笔试现场可能要多错几次 才能把情况都想到。
主要是三个情况:
* 小红没症状,药有副作用,统计加一,同时要给小红标记上症状
* 小红有症状,药治不了,同时也没副症状 ,这时也要统计加一
* 小红有症状,药可以治,给小红取消症状标记
```CPP
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, q, u;
cin >> n;
string s;
cin >> s;
cin >> m;
vector<string> a(m + 1); // 因为后面u是从1开始的
vector<string> b(m + 1);
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
}
cin >> q;
while (q--) {
cin >> u;
int num = 0;
for (int i = 0; i < n; i++) {
// s 没症状但b给了副作用统计num的同时要给s标记上症状
if (s[i] == '0' && b[u][i] == '1') {
num ++;
s[i] = '1';
}
// s 有症状,但 a治不了b也没副症状
else if (s[i] == '1' && a[u][i] == '0' && a[u][i] == '0') num++;
// s 有症状a 可以治
else if (s[i] == '1' && a[u][i] == '1') s[i] = '0';
}
cout << num << endl;
}
}
```