练习cf1644A. Doors and Keys

题目如下
A. Doors and Keys
time limit per test2 seconds
memory limit per test256 megabytes
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.

In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.

Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.

The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters:

R, G, B — denoting red, green and blue doors, respectively;
r, g, b — denoting red, green and blue keys, respectively.
Each of these six characters appears in the string exactly once.

The knight is standing at the beginning of the hallway — on the left on the map.

Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤720) — the number of testcases.

Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.

Output
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.

题目大意
现有字符串随机包含“R“,” G“,” B ”三个字母的大小写形式,大写代表门,小写代表钥匙,每个字母的大小写分别对应了对应颜色的门和能打开门的钥匙;
,先找到钥匙才能打开门,问是否能打开打开所有的门

题目分析
对应先找到钥匙才能打开门的规则,所以对应小写字母一定出现在对应大写字母之前才能打开所有门
每次输入时判断大小写,小写则存入数组,判别为大写则遍历数组若找到钥匙则进行,否则直接break

点击查看代码
for(int i = 0; i < s.length(); i++){
            if(s[i] >= 'a' && s[i] <='z'){
                key.push_back(s[i]);
            }
            if(s[i] >= 'A' && s[i] <='Z'){
                char low = tolower(s[i]);
                if(find(key.begin(), key.end(),low) == key.end()){
                    meet = 0;
                    break;
                }
            }
完整代码
点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        string s;
        cin >> s;
        vector<char> key;
        int meet = 1;
        for(int i = 0; i < s.length(); i++){
            if(s[i] >= 'a' && s[i] <='z'){
                key.push_back(s[i]);
            }
            if(s[i] >= 'A' && s[i] <='Z'){
                char low = tolower(s[i]);
                if(find(key.begin(), key.end(),low) == key.end()){
                    meet = 0;
                    break;
                }
            }
        }
        if(meet){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }
    return 0;
}
posted @ 2025-07-11 21:12  sirro1uta  阅读(11)  评论(0)    收藏  举报