题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26476
题意:给一个字符序列,比如MWMMW,每次可以取前面两个中的一个,取出来后,取出来的那个个数加一,要求使得两个字符的个数不超过n,求最多能取多少个。。
贪心就可以了。
//STATUS:C++_AC_36MS_1480KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
//#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e9+,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int n,vis[N],ans;
char s[N];
int main(){
// freopen("in.txt","r",stdin);
int i,j,l,r;
while(~scanf("%d",&n))
{
scanf("%s",s); mem(vis,);
int len=strlen(s);
int cntm,cntw;
cntm=cntw=ans=;
for(i=;i<len;i++){
l=r=-;
for(j=;j<len;j++){
if(!vis[j]){
if(l==-)l=j;
else {r=j;break;}
}
}
if(cntm<=cntw){
if(s[l]=='M')cntm++,vis[l]=;
else if(s[r]=='M')cntm++,vis[r]=;
else cntw++,vis[l]=;
}
else {
if(s[l]=='W')cntw++,vis[l]=;
else if(s[r]=='W')cntw++,vis[r]=;
else cntm++,vis[l]=;
}
if(abs(cntm-cntw)>n)break;
ans++;
} printf("%d\n",ans);
}
return ;
}