这场主要我没打,他们讨论C我手痒来了发,WA了一次后过了
过后我会写A,B,D等等的题解的,先把C的题解贴上把
C
题目大意
一个长度不超过100的字符串,对于i位置可以删去的条件是i-1或者i+1位置是它的上一个字母(比如a对于b,d对于e)
删去元素后前后两段合并,意味着i-1跟i+1相邻了
问最多能删去多少个元素
题解
主要的问题是,怎么找到最优的贪心法去暴力
很简单,如果删去的元素是r(打个比方),那么对于r-1也就是q来说,q一个也不删除,对于r来说是最优的,那么我们很容易想到,先从z开始删,删到b结束最优
注意找两边未被删去的最近的量
1 #include<bits/stdc++.h> 2 #define maxn 110 3 #define LL long long 4 using namespace std; 5 6 bool vis[maxn]; 7 8 int main(){ 9 int n; 10 string s; 11 cin >> n >> s; 12 int ans = 0; 13 for(int i = 25; i >= 0; --i){ 14 char now = 'a' + i; 15 int preans = ans; 16 for(int i = 0; i < n; ++i){ 17 int pre = i - 1, nex = i + 1; 18 while(pre >= 0 && vis[pre]){ 19 pre--; 20 } 21 while(nex < n && vis[nex]){ 22 nex++; 23 } 24 if(!vis[i] && s[i] == now && ((pre < 0 ? 0 : s[i] - s[pre] == 1) || (nex == n ? 0 : s[i] - s[nex] == 1))){ 25 ans++; 26 vis[i] = true; 27 } 28 } 29 if(ans > preans){ 30 i++; 31 } 32 } 33 cout << ans << endl; 34 return 0; 35 }