2019湘潭赛后补题 E题 XTCPC
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
xtCxtCpcpc
Sample Output
2
题意分析:在一段字符串中找到 可断固定顺序 的“xtCpc”的个数,这里有一个坑点是要多组输入。
解题思路:如果前一个字母出现的次数要多于后面的字母,那么这个字母可以作为答案中字母,当完成一个子串后删去他们;
解题思考:这道题是不可以排序的,因为我们要找 xtCpc,所以,对于字符串的子串,如果我们可以找到符合的子串,那么这个符合的子串是不会影响后面的字母排列顺序,如果这个字母在它前面字母的后面出现就可以被接受(xtCpc中,只要t在x的后面出现就可以被利用),也就是目前它前面的字母出现的比这个字母多,那么这个字母就可以利用。
#include<iostream> //Hello XTcPc
#include<cstring>
using namespace std;
char a[200005];
int main(){
int x,t,c,C,p,ans,n;
while(~scanf("%d",&n)){
x=p=t=c=C=p=0;
scanf("%s",a);
int m=strlen(a);
ans=0;
for(int i=0;i<m;i++){
if(a[i]=='x'){
x++;
}
if(a[i]=='t'&&x>t){
t++;
}
if(a[i]=='C'&&t>C){
C++;
}
if(a[i]=='p'&&C>p){
p++;
}
if(a[i]=='c'&&p>c){
c++;
}
}
while(x&&t&&C&&p&&c){
x--;
t--;
C--;
p--;
c--;
ans++;
}
printf("%d\n",ans);
}
}