欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1846G.Rudolf and CodeVid-23
Rudolf and CodeVid-23
A new virus called "CodeVid-23" has spread among programmers. Rudolf, being a programmer, was not able to avoid it.
There are symptoms numbered from to that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought medicines.
For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known.
After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy.
Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible.
Input
The first line contains a single integer — the number of test cases.
Then follow the descriptions of the test cases.
The first line of each test case contains two integers — the number of symptoms and medicines, respectively.
The second line of each test case contains a string of length consisting of the characters and — the description of Rudolf's symptoms. If the -th character of the string is , Rudolf has the -th symptom, otherwise he does not.
Then follow lines — the description of the medicines.
The first line of each medicine description contains an integer — the number of days the medicine needs to be taken.
The next two lines of the medicine description contain two strings of length , consisting of the characters and — the description of the symptoms it removes and the description of the side effects.
In the first of the two lines, at position means that the medicine removes the -th symptom, and otherwise.
In the second of the two lines, at position means that the -th symptom appears after taking the medicine, and otherwise.
Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects.
The sum of over all test cases does not exceed .
Output
For each test case, output a single integer on a separate line — the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output .
Note
In the first example, we can first apply medicine number , after which the symptoms will look like "00101". After that, medicine number , then all symptoms will disappear, and the number of days will be . Another option is to apply the medicines in the order . In this case, all symptoms will also disappear, but the number of days will be .
In the second example, there are no symptoms initially, so the treatment will take days.
In the third example, there are no options to remove all symptoms.
Samples
4
5 4
10011
3
10000
00110
3
00101
00000
3
01010
00100
5
11010
00100
4 1
0000
10
1011
0100
2 2
11
2
10
01
3
01
10
2 3
11
3
01
10
3
10
00
4
10
01
8
0
-1
6
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |