HDU6536 hello 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。

输入
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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值