Hidden String

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

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
 

题意:给你一个串,要匹配anniversary,字段数不得大于3;

题解:吐槽一下,为毛是大于等于11就ac,大于等于12就wa,错了N次。。。。。明明长度是11但是就应该到12

的啊。。。

思路:从当前开始向后匹配;匹配完成就往下深搜,当匹配段数大于3

的时候就结束当前深搜。。。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
#define T_T while(T--)
typedef long long LL;
const int INF=0x3f3f3f3f;
char s[110];
char a[20]="anniversary";
int ans,len;
void dfs(int p1,int p2,int num){
if(num>3)return;
if(p2>=11){
// printf("%d\n",num);
ans=1;return;
}
// printf("%d\n",len);
// if(p1>len)return;
int x,y;
for(int i=p1;i<len;i++){
x=i;y=p2;
while(s[x]==a[y])x++,y++;
// printf("%d",y);
if(x!=i)dfs(x,y,num+1);
else dfs(x+1,y,num+1);
}
}
int main(){
int T;
SI(T);
T_T{
ans=0;
scanf("%s",s);
len=strlen(s);
dfs(0,0,0);
if(ans)puts("YES");
else puts("NO");
}
return 0;
}

java:

package com.lanqiao.week1;

import java.util.Scanner;

public class hdu5311 {
private static Scanner cin = null;
static{
cin = new Scanner(System.in);
}
static char[] mstr = "anniversary".toCharArray();
static boolean ans;
private static void dfs(int m, int s, int cnt, char[] str){ //System.out.println(m + "-->" + s + "-->" + cnt);
if(cnt > 3)return;
if(m >= mstr.length){
ans = true;
return;
}
for(int i = s; i < str.length; i++){
int si = i, mi = m;
while(mi < mstr.length && si < str.length && mstr[mi] == str[si]){
mi++;
si++;
}
if(si != i){
dfs(mi, si, cnt + 1, str);
} }
} public static void main(String[] args) {
int T;
T = cin.nextInt();
while(T-- > 0){
String str = cin.next();
ans = false;
dfs(0, 0, 0, str.toCharArray()); if(ans){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}

Hidden String(深搜)的更多相关文章

  1. 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)  ...

  2. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  3. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  4. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  5. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  6. 【wikioi】1049 棋盘染色(迭代深搜)

    http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...

  7. HDU 4597 Play Game(记忆化搜索,深搜)

    题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...

  8. poj 3249 Test for Job (记忆化深搜)

    http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  9. 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集

    最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...

随机推荐

  1. spring mvc 非注解形式

    目录(?)[+] webxml配置文件 注如果使用注解可以加上-- servlet上下文配置文件 test-servletxml 实体类Empjava StartController控制器 控制器Em ...

  2. PHP学习遇到的问题

    使用php ob_start()模板生成html 内容无法撑开 生成后主要的内容 <body><warp><main> 生成后这个几个节点总是固定大小,不能被内容撑 ...

  3. div模拟滚动条

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="html.aspx.cs&q ...

  4. js写的简单轮播图

    这个轮播图代码是从网上找来的,专门找了个写法简单的,只是作为一个小练习,大概原理如下: 1.首先是图片切换2.自动播放3.调用自动播放4.移动到容器上边停止播放,离开自动播放5.移动到导航上停止播放, ...

  5. C语言负数的除法和求余运算

    假定我们让 a 除以 b,商为 q,余数为 r: q = a / b; r = a % b; 这里,不妨假定 b 大于 0. 我们希望 a.b.q.r 之间维持怎样的关系呢? 1.最重的一点,我们希望 ...

  6. 关于CSS动画几点要注意的地方

    关于CSS动画几点要注意的地方 js操作transition无效果 先看这个demo以及stackoverflow的问题 http://jsfiddle.net/ThinkingStiff/QNnnQ ...

  7. Qt中如果通过QStyle自定义能够跨平台的界面控件

    我们经常会碰到需要定制界面控件的要求.如果只是在一个平台上,比如说你的控件只需要在Windows上显示,那很好办,Hard code 你的look and feel就可以了.但是如果界面需要在不同平台 ...

  8. eclipse及Java常用问题及解决办法汇总

    junit-test 我觉得这点比idea好用,可以直接选中要测试的方法名,右击run as即可 http://www.cnblogs.com/brolanda/p/4532779.html 打开您的 ...

  9. Java和Android开发IDE---IntelliJ IDEA使用技巧(转)

    以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试用idea来开发. 这篇文章主要学习了idea的使用技巧. IDEA 全称 In ...

  10. Yii2简单纪要

    网上经常拿Yii来类比ROR,从MVC角度,使用体验及代码风格上确实有很多相似的地方.不过看配置文件发现Yii2不止是受rails的影响,同样有不少spring的影子,最明显的就是配置文件中很多IOC ...