zoj 3983 Crusaders Quest(思维)

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

【题目】

Crusaders Quest

【题解】

题意:给定一个长度为9的字符串,只包含3个a、3个g、3个o,可任意消除,而消除长度为3的连续字符相同的子串可触发超级技能,输出最多可触发的超级技能次数。

思路:冷静分析一下,首先把连续三个字符相同的子串消除,处理后若是长度为0则输出3,长度为6则输出2,长度为9则可能为1或者2。长度为9时需判断是否任意第一个和第三个相同字符之间都存在两个不同字符,若存在,则输出1,否则输出2。

【代码】

#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;
   string a;
   while(t--)
   {
       cin>>a; int l=9;
       go(k,1,3)
       {
           go(i,0,l-3)
           {
               if(a[i]==a[i+1]&&a[i+1]==a[i+2])
               {
                   go(j,i,l-3) a[j]=a[j+3];
                   l-=3;break;
               }
           }
       }
       int ans;
       if(l==0) ans=3;
       else if(l==6) ans=2;
       else
       {
           ans=2;
           int f=0;
           char c[]="ago";
           go(k,1,3)
           {
               int c1=0,c2=0,c3=0;
               go(i,0,8)
               {
                   if(a[i]==c[k%3]) c1++;
                   else if(c1)
                   {
                       if(a[i]==c[(k+1)%3]) c2++;
                       else c3++;
                   }
                   if(c1==3)
                   {
                       if(c2&&c3)
                        f++;
                       break;
                   }
               }
           }
           if(f==3) ans=1;
       }
       cout<<ans<<endl;
   }
}

猜你喜欢

转载自blog.csdn.net/qq_41117236/article/details/81985258
ZOJ