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");
}
}
}
}

最新文章

  1. C++序列化、反序列化
  2. ORM框架通过映射(反射)获取数据库的数据
  3. MySQL Python教程(1)
  4. Android各组件/控件间通信利器之EventBus
  5. JS,Jquery,ExtJs不同脚本动态创建DOM对象
  6. 一步一步教你将普通的wifi路由器变为智能广告路由器
  7. 第2章 linux文件系统
  8. 加密解密(10)常见HASH算法:MD5(128bit),SHA1(160bit)
  9. hadoop filesystem 删除文件 复制文件 重命名文件
  10. Android开发之Bitmap的高效加载
  11. SQL SERVER 自动生成 MySQL 表结构及索引 的建表SQL
  12. D3---01基础的柱状图制作(转)
  13. python学习第24天
  14. Wireshark常用过滤命令
  15. war的创建
  16. 使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)
  17. 【原创 深度学习与TensorFlow 动手实践系列 - 4】第四课:卷积神经网络 - 高级篇
  18. python第九十六天 ---Django(1)
  19. lsb隐写
  20. [转]抓取当前登录用户登录密码的工具:mimipenguin

热门文章

  1. 详解new/delete(整合)
  2. HOJ1008
  3. Linux命令之修改主机名
  4. Log4net 自定义字段到数据库(二)
  5. Java处理java.util.ConcurrentModificationException异常
  6. 纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果
  7. mysql 主从实现
  8. String、StringBuffer和StringBuild的区别
  9. POJ训练计划1459_Power Network(网络流最大流/Dinic)
  10. ajax验证码检测