zoj 3985 String of CCPC(字符串操作)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41117236/article/details/81990313

【题目】

String of CCPC

【题解】

题意:除了第一次操作,其他添加一个p或者c的操作都需要花费1精力,得到一个ccpc生成1精力,输出最大精力。

解析:首先求出原始字符串中所有的ccpc的个数记为ans,很显然,不可能通过一个操作生成两个ccpc,因此只可能进行第一次操作,只需要判断是否存在"cpc","ccp","ccc"即可,如果存在则ans+1。注意要排除"cccpc"的干扰。

【代码】

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define mem(a) memset(a,0,sizeof(a))
#define go(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
main()
{
    int t; cin>>t;
    while(t--)
    {
        int l; cin>>l;
        string s,t; cin>>s;
        int ans=0;
        bool f= false;
        go(i,0,l-1)
        {
            if(i+5<=l)
            {
                t=s.substr(i,5);
                if(t=="CCCPC")
                {
                    ans++;
                    i+=3;
                    continue;
                }
            }
            if(i+4<=l)
            {
                t=s.substr(i,4);
                if(t=="CCPC") ans++,i+=2;
            }
            if(!f&&i+3<=l)
            {
                t=s.substr(i,3);
                if(t=="CPC"||t=="CCP"||t=="CCC")
                    f=true;
            }
        }
        if(f) ans++;
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41117236/article/details/81990313
今日推荐