https://codeforces.com/contest/2045/problem/A
思路:
由于Y有两种选择,NG也是,那我们可以枚举以下情况:选i个Y做辅音,j个NG做辅音
然后贪心选择最长的即可,观察到S最长为5000,即使是也不会超时
具体解释在代码中
代码:
#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define ll long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endlmap<char,int> cnt;void solve()
{string s;cin >> s;for (char c : s) cnt[c]++;int ans = 0;for (int i = 0; i <= cnt['Y']; i++) {//用j个NGfor (int j = 0; j <= min(cnt['N'], cnt['G']); j++) {int vcnt = i + cnt['A'] + cnt['O'] + cnt['E'] + cnt['I'] + cnt['U'];int ccnt = s.size() - vcnt - j;//减j是因为NG将N+G合并了,所以会少一个辅音int wcnt = min(vcnt, ccnt / 2);ans = max(ans, wcnt * 3 + min(2 * wcnt, j));//用了j个NG,每一个NG都会多奉献一个长度//min(2*wcnt,j)指的是在 选的 2*wcnt 个辅音中能有多少个NG}}cout << ans << endl;
}int main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;//cin >> t;while (t--){solve();}return 0;
}