Problem Description
You have a string of lowercase letters.You need to find as many sequence “xtCpc” as possible.But letters in the same position can only be used once。
Input
The input file contains two lines.
The first line is an integer n show the length of string.(1≤n≤2×105)
The second line is a string of length n consisting of lowercase letters and uppercase letters.
Output
The input file contains an integer show the maximum number of different subsequences found.
Sample Input
10
gdCgdCpcpc
Sample Output
2
题意:找串中出现xtCpc(不一定连续)的最大个数。
思路:找一个数组(4个数x1,x2,x3,x4)分别记录x、t、 C、 p、 c的个数,数量加一当且仅当该字符前面的数量大于当前字符的数量,如果遇到c,就将数量都减一,ans++。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const int M=2e5+10;
int a[100];
char s[M];
int main()
{
int n;
while(~scanf("%d",&n))
{
mem(s,0);
scanf("%s",s);
mem(a,0);
int ans=0;
for(int i=0; i<n; i++)
{
if(s[i]!='x'&&s[i]!='t'&&s[i]!='C'&&s[i]!='p'&&s[i]!='c')
continue;
if(s[i]=='x')
s[i]='1';
else if(s[i]=='t')
s[i]='2';
else if(s[i]=='C')
s[i]='3';
else if(s[i]=='p')
s[i]='4';
else if(s[i]=='c')
s[i]='5';
if(a[s[i]-1]||s[i]=='1'){
if(a[s[i]-1]>a[s[i]]||s[i]=='1')
a[s[i]]++;
if(s[i]=='5'){
for(int j='1'; j<='5'; j++)
a[j]--;
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}