动态规划法:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
string ef;
cin>>n;
cin>>ef;
vector<int> value(n,-1);
for(int i=0;i<ef.size();i++){
if(ef[i]=='E')
value[i]=1;
}
int pre=0;
int cmax=value[0];
for(int i=0;i<n;i++){
pre=max(value[i],pre+value[i]);
cmax=max(cmax,pre);
}
cout<<cmax<<endl;
return 0;
}
少峰的置零法,太强了!
#include<iostream>
#include<vector>
using namespace std;
int main() {
string s;
int len;
cin >> len;
cin >> s;
int ans = 0;
int sum = 0;
for (int i = 0; i < len; i++) {
if (s[i] == 'E') {
sum++;
ans = max(ans, sum);
}
else {
sum--;
sum = max(0, sum);
}
}
return ans;
}