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;
}