Hidden String(深搜)
Hidden String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1679 Accepted Submission(s): 591
1. 1≤l1≤r1<l2≤r2<l3≤r3≤n
2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is "anniversary".
There's a line containing a string s (1≤|s|≤100) consisting of lowercase English letters.
annivddfdersewwefary
nniversarya
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(深搜)的更多相关文章
- 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) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 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 ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 【wikioi】1049 棋盘染色(迭代深搜)
http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
- HDU 4597 Play Game(记忆化搜索,深搜)
题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集
最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...
随机推荐
- 对常量的引用(reference to const)的一般用途(转载)
如果是对一个常量进行引用,则编译器首先建立一个临时变量,然后将该常量的值置入临时变量中,对该引用的操作就是对该临时变量的操作.对C++常量引用可以用其它任何引用来初始化:但不能改变. 关于引用的初始化 ...
- sqlserver存储过程及易错点
create PROCEDURE [dbo].[xiao_adduser] @username NVARCHAR(), @password NVARCHAR(), @adddate DATETIME ...
- Java基础之静态变量
public class StaticVariable { public static void main(String[] args) { Person p1 = new Person(); Per ...
- mysql 字段注释
create table student3(id int(30) primary key comment 'ID',name varchar(255) comment '姓名',address var ...
- python进阶4--pywin32
python 在windows下系统编程 1.环境配置:Python是没有自带访问windows系统API的库的,需要下载.库的名称叫pywin32,可以从网上直接下载. 以下链接地址可以下载: ht ...
- 通过focusInEvent和eventFilter两种方法改写控件颜色(自定义控件就是这么来的)
http://www.cnblogs.com/hicjiajia/archive/2012/05/30/2526768.html http://www.cnblogs.com/hicjiajia/ar ...
- 我的MYSQL学习心得 mysql的权限管理
这一篇<我的MYSQL学习心得(十三)>将会讲解MYSQL的用户管理 在mysql数据库中,有mysql_install_db脚本初始化权限表,存储权限的表有: 1.user表 2.db表 ...
- SGU 319 Kalevich Strikes Back(线段树扫描线)
题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...
- Windows 10 安装
下载了 Windows 10 的 ISO 文件:WindowsTechnicalPreview-x64-ZH-CN.iso,在 VMWare 10 上进行了安装. 安装时没有 Windows 10 ...
- ASP.NET快速开发框架、这才是高大上档次后台管理UI界面
另外献上在<线体验Demo地址>希望大家也能从中得到一些启发.地址:http://121.40.148.178:8080/ . 用户名:guest,密码:123456QQ技术交流群:239 ...