题目
题意:给一个字符串,寻找最多个数“xtCpc”的子串,有大小写区别。
思路:对于队列不太熟悉,一开始用数组,自己造的数据死活过不了,交了两次也就没写了。用队列保存每个字母出现的下标,然后由x开始检索,查找符合条件的下标大于前一个字母下标的对象,不符合条件的,后面的也不可能成立,于是pop。查完到‘c’的时候,答案+1;如果超出就结束。
/*
@Author : Rye Wang
@Version : 1.0
@E-mail : Rye10000010000@hotmail.com
@Site: Rye1024.gitee.io
@Time : 2020-05-24-19.05.11
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define dbug cout << "Where?" << endl;
int main()
{
int t;
string s;
while(cin >> t >> s)
{
queue<int> x;
queue<int> t;
queue<int> C;
queue<int> p;
queue<int> c;
while( ! x.empty())
x.pop();
while( ! t.empty())
t.pop();
while( ! C.empty())
C.pop();
while( ! p.empty())
p.pop();
while( ! c.empty())
c.pop();
for(int i = 0; i < s.size(); i++)
{
if(s[i] == 'x') x.push(i);
else if(s[i] == 't') t.push(i);
else if(s[i] == 'C') C.push(i);
else if(s[i] == 'p') p.push(i);
else if(s[i] == 'c') c.push(i);
}
int ans = 0;
while(x.size()){
int pos = x.front();
x.pop();
while(t.size() && t.front() < pos) t.pop();
if(t.empty()) break;
pos = t.front();
t.pop();
while(C.size() && C.front() < pos) C.pop();
if(C.empty()) break;
pos = C.front();
C.pop();
while(p.size() && p.front() < pos) p.pop();
if(p.empty()) break;
pos = p.front();
p.pop();
while(c.size() && c.front() < pos) c.pop();
if(c.empty()) break;
pos = c.front();
c.pop();
ans++;
}
cout << ans << endl;
}
return 0;
}