C++11:谁能教我用正确的方法做对这道题?
查看原帖
C++11:谁能教我用正确的方法做对这道题?
977778
__Harry_Haiyun__楼主2023/7/10 18:01

栈·操作:

#include <bits/stdc++.h>
#define MOD 998244353
#define mod 1000000007
using namespace std;
int main()
{
	stack<char>s;
    string str;
    cin >> str;
    for (int i = 0; str[i] == '@'; i++)
    {
        if (str[i] == '(') s.push('(');
        if (str[i] == ')')
        {
            if (s.empty())
            {
                cout << "NO" << endl;
                return 0;
            }
            else s.pop();
        }
    }
    s.empty() ? cout << "YES" << endl : cout << "NO\n";
	return 0;
}

普通方法:

#include <bits/stdc++.h>
#define MOD 998244353
#define mod 1000000007
using namespace std;
int main()
{
    string s;
    cin >> s;
    int left = 0,right = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '(') left++;
        if (s[i] == ')') right++;
        if (s[i] == '@') break;  
    }
    left == right ? cout << "YES\n" : cout << "NO\n";
    return 0;
}
2023/7/10 18:01
加载中...