原题目
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。
输入
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.
输出
The input file contains an integer show the maximum number of different subsequences found.
Sample Input
10
xtCxtCpcpc
Sample Output
2
题解
首先这道题是告诉我们一个字符串,然后去求里面有几个"xtCpc",并且也明确告诉了我们每个字符只能被用到一次。那这样就很简单了,我们可以在读取字符串的时候对字符进行比较。就比如,字符如果为"x",那它就是我们要找的字符的第一个(这时我们建立一个数组来储存可搭配的数量),就给数组(假设数组p)p[0]++。往后面,字符如果为"t",这时我们有两种操作:1.当p[0]>0,p[0]–,p[1]++来确保"t"的前面有与之相搭配的"x"。2.当p[0]>p[1],p[1]++其实和上一个的做法差不多。往后面"C" “p” “c”,执行与"t"相等的操作,也就是判断前面是否存在与之相搭配的字符。最后输出最后一个字符搭配成功的数量也就是"xtCpc"的数量。
#include<iostream>
#include<string>
int a,aba[6];
string s1, s2("xtCpc");
int main(){
while(cin >> a){
cin >> s1;
memset(aba, 0, sizeof aba);
for (int i = 0; i < a;i++){
if(s1[i]==s2[0])aba[1]++;
else if(s1[i]==s2[1]&&aba[2]<aba[1])aba[2]++;
else if(s1[i]==s2[2]&&aba[3]<aba[2])aba[3]++;
else if(s1[i]==s2[3]&&aba[4]<aba[3])aba[4]++;
else if(s1[i]==s2[4]&&aba[5]<aba[4])aba[5]++;
}//这里利用for倒是可以减少代码的长度
cout << aba[5] << endl;
}
system("pause");
}