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

http://acm.hdu.edu.cn/showproblem.php?pid=5311

Hidden String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1499    Accepted Submission(s): 534

Problem Description

Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a string s of length n. He wants to find three nonoverlapping substrings s[l1..r1], s[l2..r2], s[l3..r3] that:

1. 1≤l1≤r1<l2≤r2<l3≤r3≤n

2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is "anniversary".

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

There‘s a line containing a string s (1≤|s|≤100) consisting of lowercase English letters.

Output

For each test case, output "YES" (without the quotes) if Soda can find such thress substrings, otherwise output "NO" (without the quotes).

Sample Input

2
annivddfdersewwefary
nniversarya

Sample Output

YES
NO

Source

BestCoder 1st Anniversary ($)

题目大意:给两个字符串s,str[] = "anniversary";

将str分成三个部分问这三个部分能否在s中找到

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define N 110

using namespace std;

int len;
char s[N], str[] = "anniversary";

bool DFS(int d, int l1, int l2)//d表示搜索的层次,l1表示从s的l1位置处开始,l2表示str从l2位置处开始,二者进行比较
{
    int i, a, b;
    if(l2 == 11)
        return true;//搜到str的完整序列,且之前搜索的层次不大于3
    if(d > 3)
        return false;//搜索的层次大于3则返回false
    for(i = l1 ; i < len ; i++)
    {
        a = i;//表示从s的a位置处开始
        b = l2;//表示从str的b位置处开始
        if(s[i] == str[l2])
        {
            while(s[a] == str[b] && a < len && b < 11)
            {
                a++;
                b++;
            }
            if(DFS(d + 1, a, b))
                return true;
        }
    }
    return false;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", s);
        len = strlen(s);
        if(DFS(1, 0, 0))
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
时间: 2025-01-10 02:54:19

hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)的相关文章

hdu 5311 Hidden String dp o(n)算法 深搜

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 857    Accepted Submission(s): 322 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

HDU 5311 Hidden String (优美的暴力)

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 52    Accepted Submission(s): 25 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a

(BC 一周年)hdu 5311 Hidden String

Hidden String Accepts: 437 Submissions: 2174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为nn的字符串ss. 他想要知道能否找到ss的三个互不相交的子串s[l_1..r_1]s[l?1??..r?1??], s[l_2..r_2]s[l?2??..r?2

HDU 5311 Hidden String (暴力)

题意:今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下列条件: 1. 1≤l1≤r1<l2≤r2<l3≤r3≤n 2. s[l1..r1], s[l2..r2], s[l3..r3]依次连接之后得到字符串"anniversary". 思路:其实就是要在一个串中找可能存在的3个连续子串来构成这个"anniversary&q

Hdu 5311 Hidden String

一道查找字符串的题,要求在给出的字符串中找出三段字符串a,b,c,其中a,b,c三个字符串有先后关系,且不能有交集,即原字符串中的一个字母不能被用两次. 这三个字符串拼成“anniversary”. 先后A了两次,第一次用<cstring>头文件中的strncpy和strstr函数.我再昨天也写了一篇随笔,简单介绍了这两个函数的使用方法,可移步至此. 为了确保先后关系,比如“versaryanni”,虽然可以找到“versary”和“anni”但顺序不对,所以不能,必须依次寻找,且第二次和第三

hdu 5311 Hidden String(dfs)(好题)

题意:匹配串为"anniversary",对于每个给定串,为是否存在不多于3段连续子串能拼成匹配串:(n<=100) 思路:两个串从首部开始搜,若匹配次数不大于3则成功,否则不成功: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m,len,flag; char ch[15]="anniversary";

HDU 2553 N皇后问题(递归深搜)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8525    Accepted Submission(s): 3802 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求

HDU 5310 Hidden String(暴力枚举)

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 429    Accepted Submission(s): 161 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

hdoj 5311 Hidden String 【KMP + 暴力】

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1434    Accepted Submission(s): 514 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, get