题解:P13593 [NWRRC 2023] Missing Vowels

· · 题解

题目传送门

声明:此题需要使用字符串,请先学习此知识点。

废话不少说,咱们书归正题。为了方便比较字符串 s 和 f,先全部转为小写,随后建立两个指针 i(指向 s ),j(指向 f )。 而现在就需要处理两种情况:

1.如果是辅音,则必须与 s\left[i\right] 匹配,然后 i 和 j 都加 1。

2.如果是元音,选择跳过(不匹配),此时 j 加 1,i 不变。

后面就超级简单了,same 和 different 一目了然。

AC code:

#include <iostream>
#include <string>
#include <cctype> 

using namespace std;

bool isVowel(char c) {//看是不是元音
    c = tolower(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y';
}

int main() {
    string s, f;
    getline(cin, s);
    getline(cin, f);

    int i = 0; 
    int j = 0; 
    int len_s = s.length();//记录长度
    int len_f = f.length();

    while (i < len_s && j < len_f) {//情况判断
        if (tolower(s[i]) == tolower(f[j])) {
            i++;
            j++;
        } else {
            if (isVowel(f[j])) {
                j++;
            } else {
                cout << "Different" << endl;//代表多出来一个辅音 直接kill
                return 0;
            }
        }
    }

    while (j < len_f) {
        if (!isVowel(f[j])) {
            cout << "Different" << endl;//同理 直接double kill
            return 0;
        }
        j++;
    }

    if (i == len_s) {
        cout << "Same" << endl;
    } else {
        cout << "Different" << endl;//
    }

    return 0;
}

甚至代码里都没出现判断辅音和 - ,简单至极!