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. JS操作JSON的方法总结

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  2. 【转】在Spring中基于JDBC进行数据访问时怎么控制超时

    http://www.myexception.cn/database/1651797.html 在Spring中基于JDBC进行数据访问时如何控制超时 超时分类 超时根据作用域可做如下层级划分: Tr ...

  3. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

  4. WSDL规则解释(转)

    转自:http://www.blogjava.net/baoyaer/articles/116413.html WSDL文档可以分为两部分.顶部分由抽象定义组成,而底部分则由具体描述组成.抽象部分以独 ...

  5. 双线服务器和CDN的区别

    双线机房是在国内这样特殊的网络环境下特有的产物,双线机房只能解决网通和电信访问瓶颈的问题,网络传输中的链路问题和其它各ISP互通问题还是没有得到解决. CDN的原理是边缘访问,访问者就近取数据,这样大 ...

  6. 怎样注册uber司机 如何注册uber司机 最新详细攻略

    怎样注册uber司机 如何注册加入uber司机 全国加入Uber 的要求 车辆要求:要求裸车价8万以上,车龄5年以内,第三者责任险保额30万以上,不支持20万以下的面包车/商务车,不支持4座以下车辆. ...

  7. java常用日期函数总结

    请记得要引入java.util.Date和java.text.SimpleDateFormat两个包 1.计算某一月份的最大天数 Calendar time=Calendar.getInstance( ...

  8. 飘逸的python - 用urlparse从url中抽离出想要的信息

    最近有个需求,要检测配置中的那些url的域名是否都正常,即是否都能ping通. 不过配置中url格式是这样的 http://www.something.com:1234/ . 要ping的是www.s ...

  9. svn+ssh

    According to official document, svn+ssh is supposed to be somehow faster than apache+dav_svn, howeve ...

  10. 简单的web三层架构系统【第一版】

    SQLhelper助手类编写: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Sys ...