题解:P13593 [NWRRC 2023] Missing Vowels

· · 题解

题目链接:P13593 [NWRRC 2023] Missing Vowels

题目意思: 判断短名称 s 是否可以通过从全名称 f 中省略一些元音字母(可能为零个)得到。

解题思路:

用双指针的方法实现,时间复杂度O(n) (n为较长字符串的长度)。

注意事项:

  1. 元音字母是 a , e , i , o , uy 六个字母,而不是我们平时的五个!
  2. ‌不区分大小写‌:比较时统一转换为小写或大写!

你们最最期待的代码

#include<bits/stdc++.h>
using namespace std;
// 判断字符是否是元音字母
bool judge(char c) 
{
    c = tolower(c);
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y');
}
// 检查s是否可以通过从f中省略元音字母得到
bool check(string s, string f) 
{
    int i = 0 , j = 0;
    while(i < s.size() && j < f.size()) 
    {
        if(tolower(s[i]) == tolower(f[j]))
        {
            i++;
            j++;
        } 
        else 
        {
            if(judge(f[j])) 
                j++;
            else
                return false;
        }
    }
  // 检查f中剩余字符是否都是可以省略的元音字母
    while(j < f.size()) 
    {
        if(!judge(f[j])) 
            return false;
        j++;
    }
    return (i == s.size());
}
int main() 
{
    string s , f;
    cin >> s >> f;
    if(check(s , f)) 
        cout << "Same";
    else 
        cout << "Different";
    return 0;//完结撒花
}

管理员大大求过qaq(⑅˃◡˂⑅)