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";
char str[500010];
void dfs(int a,int b,int step)
{
    if(step<=3&&!ch[b]){ //匹配串只遍历一次
       flag=1;return;
    }
    if(step>3||!ch[b]||!str[a]) return;
    dfs(a+1,b,step);
    if(str[a]==ch[b]){
      while(str[a]==ch[b]&&str[a]&&ch[b]){
         a++;b++;
      }
      dfs(a,b,step+1);
    }
}
int main()
{
    int i,j,k;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            flag=0;
            scanf("%s",str);
            len=strlen(str);
            dfs(0,0,0);
            if(flag) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
时间: 2024-11-12 07:39:55

hdu 5311 Hidden String(dfs)(好题)的相关文章

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 anniversar

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 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

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

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 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

HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27185    Accepted Submission(s): 6630 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方