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”。但是相同位置的字母只能使用一次。
错误代码
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n;
char str1[200005];
int i;
cin>>n;
scanf("%s",str1);
int cn[5]={0};
for(i=0;i<n;i++)
{
if(str1[i]=='x')
cn[0]++;
else if(str1[i]=='t')
cn[1]++;
else if(str1[i]=='C')
cn[2]++;
else if(str1[i]=='p')
cn[3]++;
else if(str1[i]=='c')
cn[4]++;
}
int Min=200005;
for(i=0;i<5;i++)
{
if(Min>=cn[i])
Min=cn[i];
}
cout<<Min<<endl;
return 0;
}
出错原因:忽略了“xtCpc"要有顺序
正确代码
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n;
char str[200005];
while(cin>>n)
{
scanf("%s",str);
queue<int>q[5];
for(int i=0; i<n; i++)
{
if(str[i]=='x')
q[0].push(i);
else if(str[i]=='t')
q[1].push(i);
else if(str[i]=='C')
q[2].push(i);
else if(str[i]=='p')
q[3].push(i);
else if(str[i]=='c')
q[4].push(i);
}
int cn=0;
int flag=0;
while(!q[0].empty())
{
int k=q[0].front();
q[0].pop();
for(int i=1; i<5; i++)
{
while(!q[i].empty()&&q[i].front()<k)
{
q[i].pop();
}
if(q[i].empty())
{
flag=1;
break;
}
k=q[i].front();
q[i].pop();
}
if(flag)
break;
cn++;
}
cout<<cn<<endl;
}
return 0;
}