BestCoder 1st Anniversary ($) Hidden String(深搜)

题意:给一个字符串s,问s中先后出现的三个子串是否能组成“anniversary”


解析:深搜,搜的层次小于等于三而且找到完整字符“anniversary”即正确,否则错误

 


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1e6;

char s[105];
int n;
char m[]={"anniversary"};
bool dfs(int d,int st1,int st2)         //d表示当前搜索的层次。st1表示当前从s串的st1位置開始查找,st2表示当前从“anniversary”的st2位置開始查找
{
    if(st2==11) return true;        //找到完整序列且之前的搜索层次小于等于3,返回true
    if(d>=4) return false;          //搜索三次还没找到。返回false
    int tmp1,tmp2;
    for(int i=st1;i<n;i++)
    {
        tmp2=st2;
        tmp1=i;
        if(s[i]==m[st2]){
            while(s[tmp1]==m[tmp2]&&tmp1<n&&tmp2<11){
                tmp1++;
                tmp2++;
            }
            if(dfs(d+1,tmp1,tmp2)) return true;
        }
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        n=strlen(s);
        if(dfs(1,0,0)) printf("YES\n");
        else printf("NO\n");
    }
}



posted @ 2017-06-04 09:56  jzdwajue  阅读(100)  评论(0编辑  收藏  举报